添加两个大数字(128位)。
SYSEXIT = 1
EXIT_SUCCESS = 0
.data
number1:
.long 0x10304008, 0x701100FF, 0x45100020, 0x08570030
number2:
.long 0xF040500C, 0x00220026, 0x321000CB, 0x04520031
.text
.global _start
_start:
addition:
movl $4, %ecx
clc
add_loop:
movl %ecx, %edx
decl %edx
movl number1(,%edx,4), %eax
movl number2(,%edx,4), %ebx
adcl %eax, %ebx
pushl %ebx
loop add_loop
mov $SYSEXIT, %eax
mov $EXIT_SUCCESS, %ebx
int $0x80
我使用x/5wx $esp
在gdb下检查它,并且我始终00000001
作为第5个字,即使我更改0x1040500C, 0x00220026, 0x321000CB, 0x04520031
的number2以防止进位。
怎么了?
答案 0 :(得分:1)
您只存储4个单词,那么为什么您希望第5个单词具有任何特定值?
顺便说一下,1
应该是传递给你的程序的参数的数量,其中第一个参数通常是程序本身的名称。
答案 1 :(得分:0)
正如Jester在他的回答中提到的那样,你并没有试图将进位位的值推到堆栈上,所以当你寻找它时,你没有看到它 - 设置或其他 - 。在00000001
被调用之前,_start
被放置在那里。
您可以使用setc
将进位标志的值设置为寄存器或内存位置,如果需要,可以按下该值。这是一个例子,对64位汇编进行了一些更改:
SYSEXIT = 1
EXIT_SUCCESS = 0
.global _start
.section .data
number1: .long 0x10304008, 0x701100FF, 0x45100020, 0x08570030
number2: .long 0xF040500C, 0x00220026, 0x321000CB, 0x04520031
carryval: .long 0x00
.section .text
_start:
movl $4, %ecx
clc
add_loop:
movl %ecx, %edx
decl %edx
movl number1(,%edx,4), %eax
movl number2(,%edx,4), %ebx
adcl %eax, %ebx
setc carryval # Added this line
push %rbx # Changed for 64 bit
loop add_loop
movl carryval, %eax # Added this line
push %rax # Added this line
mov $SYSEXIT, %eax
mov $EXIT_SUCCESS, %ebx
int $0x80
使用gdb输出:
paul@thoth:~/src/asm$ gdb ./bigint
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/paul/src/asm/bigint...done.
(gdb) b 29
Breakpoint 1 at 0x4000df: file bigint.s, line 29.
(gdb) run
Starting program: /home/paul/src/asm/bigint
Breakpoint 1, add_loop () at bigint.s:29
29 mov $SYSEXIT, %eax
(gdb) x/5xg $rsp
0x7fffffffe518: 0x0000000000000001 0x0000000000709014
0x7fffffffe528: 0x0000000070330125 0x00000000772000eb
0x7fffffffe538: 0x000000000ca90061
(gdb)
您可以看到最高值现在设置为1
,表示您的进位已设置。
如果您将number2
的第一个双字更改为0x1040500C
,那么您将获得此信息:
paul@thoth:~/src/asm$ gdb ./bigint
GNU gdb (GDB) 7.4.1-debian
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/paul/src/asm/bigint...done.
(gdb) b 29
Breakpoint 1 at 0x4000df: file bigint.s, line 29.
(gdb) run
Starting program: /home/paul/src/asm/bigint
Breakpoint 1, add_loop () at bigint.s:29
29 mov $SYSEXIT, %eax
(gdb) x/5xg $rsp
0x7fffffffe518: 0x0000000000000000 0x0000000020709014
0x7fffffffe528: 0x0000000070330125 0x00000000772000eb
0x7fffffffe538: 0x000000000ca90061
(gdb)
显示您的进位标志未设置,正如预期的那样。