3

I can't figue out why my program goes into an infinite loop when i want it to exit after the value of ecx is equal to 0? Please help?

section .data
;get external functions
extern printf
global main
main:

;set up stack frame
push rbp
mov rbp, rsp

;if(x<y)
;print x is less
;else
;print y is larger than x

;mov values into register to compare them
mov rax,[x]
mov rbx,[y]
cmp rax,rbx ;cmp x,y
jg .x_is_greater
lea rdi,[y_less]
xor eax,eax ;must clear eax when using printf
call printf
jmp .done

.x_is_greater:
;print "X is greater to the screen"

;mov r11,[count]
;lea rdi,[x_greater]
;xor eax,eax
;call printf
;mov r12,[zero]
;cmp r11,r12
;jg .myloop ;jump to myloop if greater than zero
;jmp .done ;return if equal to 0
mov ecx, 3; [count]
;mov r12, [zero]
jmp .myloop
.myloop:
;;dec r11
;dec rcx
lea rdi,[fmt]
lea rsi,[ecx]   
;mov rdx,[r12]
xor eax,eax ;must clear eax when using printf
call printf

cmp ecx, 0
jz .done
lea rdi,[x_greater]
xor eax,eax ;must clear eax when using printf
call printf
lea rdi,[fmt]
lea rsi,[ecx]   
;mov rdx,[r12]
xor eax,eax ;must clear eax when using printf
call printf
dec ecx

;sub rcx,[one]
jmp .myloop
;jmp .done
.done:
leave
;xor eax, eax
ret;exit program

;leave ;destroy stack frame

section .bss

section .data
prompt db "This is a practice program to test what I know!",0x0a,0
y_less db "Y < X",0x0a,0
x_greater db "X > Y ",0x0a,0
x db 10
y db 1
count dq 3
zero db 0
one dq 1
fmt db "R11 %d ",0x0a,0
Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
James
  • 77
  • 7

3 Answers3

5

When calling functions (e.g., printf), you need to preserve the value of ecx

http://www.x86-64.org/documentation/abi.pdf

Registers %rbp, %rbx and %r12 through %r15 “belong” to the calling function and the called function is required to preserve their values. In other words, a called function must preserve these registers’ values for its caller. Remaining registers “belong” to the called function. If a calling function wants to preserve such a register value across a function call, it must save the value in its local stack frame.

cbranch
  • 4,709
  • 2
  • 27
  • 25
1

The printf call may change the value of ecx register. So what you have to do one of the following:

  • Push it to the stack before the printf calls and pop it after the printf call;
  • Use a callee-saved register as the loop counter; or
  • Save it in a callee-saved register and restores it.

Example of the first option:

.myloop:
lea rdi,[fmt]
lea rsi,[ecx]
xor eax,eax ;must clear eax when using printf
push ecx ; saved
call printf
pop ecx ; restored

cmp ecx, 0
jz .done
lea rdi,[x_greater]

push ecx ; saved
xor eax,eax ;must clear eax when using printf
call printf
pop ecx ; restored

lea rdi,[fmt]
lea rsi,[ecx]

push ecx ; saved
xor eax,eax ;must clear eax when using printf
call printf
push ecx ; restored

dec ecx
jmp .myloop
Murilo Vasconcelos
  • 4,677
  • 24
  • 27
  • 1
    Regarding your comment "you don't need to do this" ... If this is 64-bit (which seems to be the case based on some of the registers being used), then you **do** need to clear `eax` when calling functions that accept a variable number of arguments. Refer to footnote #13 on page 20 [http://www.x86-64.org/documentation/abi.pdf](http://www.x86-64.org/documentation/abi.pdf). Also, there's additional discussion in section 3.5.7 starting on page 50. – cbranch Oct 27 '12 at 05:45
  • +1 didn't knew. BTW it seems that he have to clear the whole `rax`. – Murilo Vasconcelos Oct 27 '12 at 05:50
  • 1
    Technically, I think that only the low byte (`al`) is actually used, but personally I would go ahead and clear the whole `rax` just for readability. – cbranch Oct 27 '12 at 05:57
  • Yes, `xor eax,eax` is more efficient than `xor al,al` ([since it zeros the whole register](http://stackoverflow.com/questions/33666617/which-is-best-way-to-set-a-register-to-zero-in-x86-assembly-xor-mov-or-and/33668295#33668295)), so it's preferred. But yes, the ABI specifies that varargs functions must only look at the low byte of `al` as an input, as the rest of rax can contain random garbage. This lets you use `setcc al` to pass zero or one FP arg. Or save code-size with `mov al, 5` where you're passing a non-zero number of FP args in vector regs. – Peter Cordes Jun 02 '16 at 23:45
1

I have made a loop called WHILE-DO with Printf and just checked on Microsoft Visual Studio That RAX,RCX,RDX,R9,R8 are used by Call Printf. So I added our number to EBX. It's working without poping, pushing, stacking :). I hope it will help some people.

extrn ExitProcess: PROC
extrn printf: PROC

.data
fmt db '%s',10,0 ; The printf format, "\n", '0'
check db 'HALO',0

.code
Start PROC

xor eax,eax
mov rbx,1

start_while:
    cmp rbx,10
    jge end_while

    lea rcx,check
    lea rdx,fmt
    call printf


    add rbx,1
    jmp start_while
end_while:
xor eax,eax
xor rbx,rbx
call ExitProcess
Start ENDP
End
Michael Petch
  • 46,082
  • 8
  • 107
  • 198
Mayurii
  • 26
  • 3