我想知道为什么我的程序进入无限循环时我希望它在secx的值等于0后退出?请帮忙?
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
答案 0 :(得分:5)
调用函数(例如printf
)时,需要保留ecx
的值
http://www.x86-64.org/documentation/abi.pdf
注册%rbp,%rbx和 %r12到%r15“属于”调用函数,被调用函数是 需要保持他们的价值观。换句话说,被调用的函数必须保留 这些寄存器的值为其调用者。 剩余的寄存器“属于”被叫 功能。如果调用函数想要在a中保留这样的寄存器值 函数调用时,必须将值保存在本地堆栈框架中。
答案 1 :(得分:1)
printf
调用可能会更改ecx
注册的值。那么您必须做以下其中一项:
printf
来电之前将其推送至堆栈,并在printf之后将其pop
推出; 第一个选项的示例:
.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
答案 2 :(得分:1)
我使用Printf创建了一个名为WHILE-DO的循环,并在Microsoft Visual Studio上检查了Call Printf使用了RAX,RCX,RDX,R9,R8。所以我将我们的号码添加到EBX。它没有弹出,推动,堆叠:)工作。我希望它会帮助一些人。
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