第30部分-Linux x86 64位汇编 乘法/除法
乘法
无符号整数乘法mul如下,目标操作数总是eax寄存器的某种形式。

使用IMUL可以进行有符号乘法。
在只有一个操作数的情况下,结果保存到指定的目标寄存器EAX或寄存器的某种形式,这个同MUL。
IMUL支持2个操作数。
imul source,destination
IMUL支持3个操作数。
imul multiplier,source,destination
其中multiplier是一个立即值。
乘法实例
.section .data
output:
.asciz "The result is %d, %ld\n"
value1:
.int 10
value2:
.int -35
value3:
.int 400
.section .text
.globl _start
_start:
nop
movl value1, %ebx;//移动value1到ebx
movl value2, %ecx;//移动value2到ecx
imull %ebx, %ecx;//将ebx和ecx相乘结果保存到ecx
movl value3, %edx;//移动value3到edx
imull $2, %edx, %eax;//继续相差
movq $output,%rdi
movl %ecx ,%esi;//商
movl %eax ,%edx;//余数
call printf
movl $1, %eax
movl $0, %ebx
int $0x80
as -g -o imultest.o imultest_att.s
ld -o imultest imultest.o -lc -I /lib64/ld-linux-x86-64.so.2
除法
除法也和乘法类似。无符号除法使用div,有符号是idiv。
不过idiv只有一个参数值。
除法操作如下:
除法实例
.section .data
dividend:
.quad 8335
divisor:
.int 25
quotient:
.int 0
remainder:
.int 0
output:
.asciz "<93>The quotient is %d, and the remainder is %d\n<94>"
.section .text
.globl _start
_start:
nop
movl dividend, %eax
movl dividend+4, %edx
divw divisor ;//除数
movl %eax, quotient
movl %edx, remainder
movq $output,%rdi
movl quotient,%esi;//商
movl remainder,%edx;//余数
call printf
movl $1, %eax
movl $0, %ebx
int $0x80
as -g -o divtest.o divtest.s
ld -o divtest divtest.o -lc -I /lib64/ld-linux-x86-64.so.2