I've been trying to learn some assembly and was testing out arrays and found that when I tried to print out the value at the point indexed nothing happened, after experimenting further it appears that even though I am using the arrays as shown in many examples across the internet, it just simply isn't working
Here's the code:
section .text
global _start
_start:
mov eax, num ; eax now contains 5
mov ebx, [array+8] ; ebx now contains 8
cmp eax, ebx ; compares eax to ebx
jge skip ; should not happen because eax is smaller than ebx
call printdigit
skip:
call printn
call _exit
printdigit:
mov eax, 0x30
add [num], eax
mov ecx, num
mov edx, 1 ;length
mov ebx, 1 ;write to stdout
mov eax, 4 ;write call number
int 0x80
ret
printn:
mov eax, 0x0A
push eax
mov eax, 4
mov ebx, 1
mov ecx, esp
mov edx, 1
int 0x80
add esp, 4
ret
_exit:
mov eax, 1
mov ebx, 0
int 0x80
section .data
num dw 5
array dw 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
The commands I'm using to compile the code
nasm -f elf Bubblesort.asm
ld -m elf_i386 -s -o Bubblesort Bubblesort.o
./Bubblesort
What I'm running:
ubuntu 22.04.3 desktop amd64, (on virtual machine but shouldn't matter I think)
The output I want should be
5
The actual output
I want printdigit to be called only when num is less than whatever is indexed at array
I am almost certain its not a computer issue but a code issue but I'm unsure where
I have now done the following changes
_start:
mov eax, [num]
mov ebx, [array+8]
cmp eax, ebx
jae skip
section .data
num dd 5
array dd 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
the rest of the code is as normal
this now prints the value i want when array is indexed to a value greater than num, but when it isn't greater it still prints the value
Solution:
section .text
global _start
_start:
mov eax, [num]
movzx ebx, word [array+12]
cmp eax, ebx
jb skip
call printdigit
jmp exit
skip:
call printn
exit:
call _exit
printdigit:
mov eax, 0x30
add [num], eax
mov ecx, num
mov edx, 1 ;length
mov ebx, 1 ;write to stdout
mov eax, 4 ;write call number
int 0x80
ret
printn:
mov eax, 0x0a
push eax
mov eax, 4
mov ebx, 1
mov ecx, esp
mov edx, 1
int 0x80
add esp, 4
ret
_exit:
mov eax, 1
mov ebx, 0
int 0x80
section .data
num dd 3
array dw 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
mov eax, num
eax = address of num, my debugger shows value 00402000.
mov ebx, [array+8]
ebx = 00050004.
cmp eax,ebx
eax >= ebx so code jumps to skip
label.
Write function prints 0x0A
which is new line
code. If You change to 0x35
You will see 5
.
printdigit
code is never executed.