【发布时间】:2015-07-08 02:54:09
【问题描述】:
我正在汇编中编写一个 while 循环,以便在 Linux 终端中使用 nasm 和 gcc 进行编译。程序比较 x 和 y 直到 y >= x 并在最后报告循环数。代码如下:
segment .data
out1 db "It took ", 10, 0
out2 db "iterations to complete loop. That seems like a lot.", 10, 0
x db 10
y db 2
count db 0
segment .bss
segment .text
global main
extern printf
main:
mov eax, x
mov ebx, y
mov ecx, count
jmp lp ;jump to loop lp
lp:
cmp ebx, eax ;compare x and y
jge end ;jump to end if y >= x
inc eax ;add 1 to x
inc ebx ;add 2 to y
inc ebx
inc ecx ;add 1 to count
jp lp ;repeat loop
end:
push out1 ;print message part 1
call printf
push count ;print count
call printf
push out2 ;print message part 2
call printf
;mov edx, out1 ;
;call print_string ;
;
;mov edx, ecx ;these were other attempts to print
;call print_int ;using an included file
;
;mov edx, out2 ;
;call print_string ;
这是在终端中编译和运行的:
nasm -f elf test.asm
gcc -o test test.o
./test
终端输出如下:
It took
iterations to complete loop. That seems like a lot.
Segmentation fault (core dumped)
我看不出逻辑有什么问题。我认为这是语法,但我们才刚刚开始学习汇编,我已经尝试了各种不同的语法,比如变量周围的括号和在片段末尾使用 ret,但似乎没有任何效果。我还搜索了分段错误,但没有发现任何真正有用的东西。任何帮助将不胜感激,因为我是一个绝对的初学者。
【问题讨论】: