为什么我在NASM大会不工作寄存器不断比较? [英] Why is my register-constant comparison not working in NASM Assembly?
问题描述
学习NASM大会于32位的Ubuntu。
这是给我头痛的事:假设我有一个数组(我叫它在程序向量)在。数据
:
矢量数据块1,2,3,4
大小EQU $ - 矢量
和我也有一些 0
:
索引数据库0
我要存储尺寸
在 EBX
,然后存储首页
在 ECX
,最后检查 ECX< EBX
。在这个例子中,那将是 0℃; 4
。我的程序根本就没有做到这一点:
部分。数据
矢量数据块1,2,3,4
大小EQU $ - 矢量 索引数据库0 ;词汇
MSG1 DBECX< EBX,10
msg1len EQU $ -msg1
MSG 2分贝ECX> = EBX,10
msg2len EQU $ -msg2.text段
全球主要
主要:
; -----------------------------------------------
;主要
; -----------------------------------------------
MOV EBX,大小; EBX = 4
MOV ECX,[指数] ECX = 0
来电查询;检查ECX< EBX(0℃; 4); -----------------------------------------------
;出口
; -----------------------------------------------
MOV EAX,1
INT 0x80的; -----------------------------------------------
;检查
;检查是否ECX< EBX
; -----------------------------------------------
检查:
CMP ECX,EBX
JGE greater_or_equalMOV EAX,4
MOV EBX,0
MOV ECX中,MSG
MOV EDX,msg1len
INT 0x80的
JMP end_checkgreater_or_equal:MOV EAX,4
MOV EBX,0
MOV ECX,MSG 2
MOV EDX,msg2len
INT 0x80的end_check:RET
它输出:
ECX> = EBX
块引用>但是,这只是不正确的。
ECX
应该是0和EBX
应为4。我不明白这个问题 - 但我有一个观察:
MOV EBX,大小
这行的错误我 - 我想做到这一点:
MOV EBX,[尺寸]
不过,我会得到一个分段错误。我不知道会发生,但我猜,这是因为
尺寸
不是一个地址 - 它只是一个数字,所以我不能用上支架。我可能是错的(在这种情况下,这可能是问题的原因)。解决方案索引数据库0
MSG1 DBECX< EBX,10MOV ECX,[指数]
首页
定义为分贝
,所以指令将移至ECX
这个0和3从MSG 1
使用:
指数DD 0
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
inEBX
, then storeindex
inECX
, and finally check ifECX < EBX
. In this example, it would be0 < 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 andEBX
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 asdb
, so the instruction will move toecx
this 0 and 3 more bytes frommsg1
Use:
index dd 0
这篇关于为什么我在NASM大会不工作寄存器不断比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!