Learning NASM Assembly in 32-bit Ubuntu.
This is giving me headaches: suppose that I have an array (I call it vector in the program) in .data
:
vector db 1,2,3,4
size equ $-vector
And I also have a number 0
:
index db 0
I want to store size
in EBX
, then store index
in ECX
, and finally check if ECX < EBX
. In this example, it would be 0 < 4
. My program simply doesn't do it:
SECTION .data
vector db 1,2,3,4
size equ $-vector
index db 0
; Vocabulary
msg1 db "ECX < EBX",10
msg1len equ $-msg1
msg2 db "ECX >= EBX",10
msg2len equ $-msg2
SECTION .text
global main
main:
; -----------------------------------------------
; MAIN
; -----------------------------------------------
mov EBX,size ; EBX = 4
mov ECX,[index] ; ECX = 0
call check ; Check ECX < EBX (0 < 4)
; -----------------------------------------------
; EXIT
; -----------------------------------------------
mov EAX,1
int 0x80
; -----------------------------------------------
; Check
; Checks whether ECX < EBX
; -----------------------------------------------
check:
cmp ECX,EBX
jge greater_or_equal
mov EAX,4
mov EBX,0
mov ECX,msg1
mov EDX,msg1len
int 0x80
jmp end_check
greater_or_equal:
mov EAX,4
mov EBX,0
mov ECX,msg2
mov EDX,msg2len
int 0x80
end_check:
ret
It outputs:
ECX >= EBX
But that's just not right. ECX
should be 0 and EBX
should be 4.
I don't see the problem - but I do have an observation:
mov EBX,size
This line bugs me - I wanted to do this:
mov EBX,[size]
But I would get a segmentation fault. I didn't know that would happen, but I'm guessing that it is because size
isn't an address - it's just a number, so I can't use the brackets on it. I might be wrong (in which case this could be the cause of the problem).
index db 0
msg1 db "ECX < EBX",10
mov ecx, [index]
index
is defined as db
, so the instruction will move to ecx
this 0 and 3 more bytes from msg1
Use:
index dd 0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.