Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

assembly - Print out register in decimal without printf

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

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
"

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)

Update

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 "
"
sum: .byte 0, 0, 0, 0
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...