I am writing a while loop in an assembly to compile on a Linux terminal with nasm and gcc. The program compares x and y to y> = x and reports the number of cycles at the end. Here is the code:
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 ;
This is compiled and run in the terminal with:
nasm -f elf test.asm
gcc -o test test.o
./test
The terminal output is displayed as:
It took
iterations to complete loop. That seems like a lot.
Segmentation fault (core dumped)
I do not see anything wrong with logic. I think it is syntactic, but we just started to learn assembly, and I tried all kinds of syntaxes like brackets around variables, and using retat the end of the segment, but nothing works. I also looked for segmentation errors, but I did not find anything useful. Any help would be appreciated because I'm an absolute beginner.
source
share