gpt4 book ai didi

linux - 集会!我无法让循​​环退出?

转载 作者:可可西里 更新时间:2023-11-01 11:44:11 26 4
gpt4 key购买 nike

我想不通为什么我的程序会在我希望它在 ecx 的值等于 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

最佳答案

调用函数时(例如printf),需要保留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.

关于linux - 集会!我无法让循​​环退出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13097380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com