不带printf的十进制打印寄存器 [英] Print out register in decimal without printf

查看:125
本文介绍了不带printf的十进制打印寄存器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从屏幕上的寄存器EDX中打印出值. 程序应该找到最大的通体深度,例如((x))EDX = 2

I'm trying to print out value from register EDX on the screen. The program should find the maximum depth of paranthesis e.g for ((x)) EDX = 2

我不能使用stdlib. 我的程序使用stdlib

And I can't use stdlib. My program using stdlib

.intel_syntax noprefix
  .globl main
  .text

main:
pop eax #return address
pop eax #return argc
pop eax #return argv
mov eax,[eax+4] #argv[1]
sub esp,12 #return stack to the right position
lea ebx,[eax]
xor eax,eax
xor ecx,ecx
xor edx,edx


loop:
  mov al,[ebx]
  or al,al
  jz print
  cmp al,'('
  je increase
  cmp al,')'
  je decrease
  inc ebx
  jmp loop

increase:
inc ecx
cmp edx,ecx
js changeMax
inc ebx
jmp loop

changeMax:
mov edx,ecx
inc ebx
jmp loop

decrease:
dec ecx
inc ebx
jmp loop

print:
push edx
mov edx, offset mesg
push edx
call printf
add esp,8
ret
mov edx,0
ret


data:
mesg: .asciz "%d\n"

我读到,我需要使用模,然后将余数推入堆栈. 是这样做的另一种方式(处理器说了一些关于十六进制值的移位)

I read, that I need to use modulo, and push remainder into stack. Is it another way to do this (proffesor said something about shifting hexadecimal value)

这应该可以,但是我出现了细分错误

This should work, but I got segmentation fault

.intel_syntax noprefix
.text
.globl _start

_start:
op eax #return address
pop eax #return argc
pop eax #return argv
mov eax,[eax+4] #argv[1]
sub esp,12 #return stack to right position
lea ebx,[eax]
xor eax,eax
xor ecx,ecx
xor edx,edx


loop:
  mov al,[ebx]
  or al,al
  jz result 
  cmp al,'('
  je increase
  cmp al,')'
  je decrease
  inc ebx
  jmp loop

increase:
inc ecx
cmp edx,ecx
js changeMax
inc ebx
jmp loop

changeMax:
mov edx,ecx
inc ebx
jmp loop

decrease:
dec ecx
inc ebx
jmp loop

result:
mov eax, edx # moving result into eax, because of div operation

conv:
    mov ecx, 10
    xor ebx, ebx

divide:
    xor edx, edx
    div ecx
    push edx
    inc ebx
    test eax, eax
    jnz divide

next_digit:
    pop eax
    add eax, '0'
    mov [sum], eax
    dec ebx
    cmp ebx, 0
    je final
    pop eax
    add eax, '0'
    mov [sum+1], eax
    dec ebx
    cmp ebx, 0
    je final
    pop eax
    add eax, '0'
    mov [sum+2], eax
    dec ebx
    cmp ebx, 0
    je final

final:
    mov edx, 3 #length of string
    mov ecx, offset sum
    mov ebx, 1
    mov eax, 4
    int 0x80
    mov edx, 1
    mov ecx, offset msg
    mov ebx, 1
    mov eax, 4
    int 0x80
    mov eax, 1
    int 0x80

.data
msg: .ascii "\n"
sum: .byte 0, 0, 0, 0

推荐答案

好,这是我的解决方法.

Ok, it's my solution.

.intel_syntax noprefix
  .globl _start
  .text

_start:
mov eax, [esp+8] #argv[0] to eax
lea ebx,[eax]
xor eax,eax
xor ecx,ecx
xor edx,edx


loop:
  mov al,byte ptr [ebx]
  or al,al
  jz result #end of the argument 
  cmp al,'('
  je increase
  cmp al,')'
  je decrease
  inc ebx
  jmp loop

increase:
inc ecx
cmp edx,ecx
js changeMax
inc ebx
jmp loop

changeMax:
mov edx,ecx
inc ebx
jmp loop

decrease:
dec ecx
inc ebx
jmp loop

result:
mov eax, edx

base:
mov ecx,10 
xor ebx, ebx 

divide:

  xor edx, edx
  div ecx
  push edx
  inc ebx
  test eax, eax
  jnz divide

to_ASCII:
  pop eax
  add eax, '0'
  mov [sum], eax
  dec ebx
  cmp ebx, 0
  je print

  pop eax
  add eax, '0'
  mov [sum+1], eax
  dec ebx
  cmp ebx, 0
  je print

  pop eax
  add eax, '0'
  mov [sum+2], eax
  dec ebx

print:
mov eax, 4
mov ebx, 1
mov ecx, offset sum
mov edx, 3 #
int 0x80

mov eax,4
mov ebx,1
mov ecx, offset msg
mov edx,1
int 0x80

mov eax,1
mov ebx,0
int 0x80



.data
msg: .asciz "\n"
sum: .byte 0,0,0,0

这篇关于不带printf的十进制打印寄存器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆