第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