【发布时间】:2017-05-09 20:16:22
【问题描述】:
我有一个相当愚蠢的问题,但我在任何地方都找不到解决方案。
我正在尝试编写一个程序,该程序将内存中的两个数字(带进位)逐个(32 位)相加,并将部分和写入堆栈。不幸的是,不知道为什么,循环计数器在inc %ecx 之后没有增加。 (我已经在 gdb 中测试过,%ecx 甚至在 inc 指令之后也保持在 0)。
我为 Intel 架构编写,但使用的是 AT&T 语法(我在这里别无选择)。
.code32
SYSEXIT = 1
EXIT_SUCCESS = 0
.align 32
.data
nr1:
.long 0xF0000111, 0x000B0000
nr2:
.long 0x10000333, 0x000A000F
size:
.long 0x00000002
.text
.global _start
_start:
mov $0, %ecx #initialization of counter
movl size(,%ecx,4), %edi #moving size (amount of 32-bit pieces) to %edi
addition:
movl nr1(,%ecx,4), %eax #moving one piece
movl nr2(,%ecx,4), %ebx #(from address of nr1 + (value of %ecx * 4 bytes)
adcl %ebx, %eax #add with carry
push %eax #push the result
inc %ecx #increment counter
cmp %edi, %ecx #compare %edi (==2) with counter (%ecx)
je overflow # if %edi == %ecx
jmp addition # else back to addition loop
overflow: #in case when last sum had an overflow (CF==1)
mov $0, %eax
adcl $0, %eax
push %eax
end:
mov $SYSEXIT, %eax
mov $EXIT_SUCCESS, %ebx
int $0x80
如果您还可以解释它发生的原因以及将来如何避免该问题,我将不胜感激(如您所见,我仍在学习)。另外,如果您发现其他错误,请告诉我。提前致谢。
【问题讨论】:
标签: assembly x86 nasm increment intel