DIV组装 [英] Div in assembly
问题描述
我想补充这必将产生一个两位数或三位数2双位数字。
这里是我到目前为止,当我尝试打印随身携带,它说的浮点异常(核心转储)的
部分。数据
味精DB进入2号
msgLen EQU $ -msg.bss段
numa1 RESB 1
numa2 RESB 1
numb1 RESB 1
numb2 RESB 1
携带RESB 1.text段
全球_start_开始:
;打印消息
MOV EAX,4
MOV EBX,1
MOV ECX,味精
MOV EDX,msgLen
INT 80H ;接受第一号(第1位)
MOV EAX,3
MOV EBX,0
MOV ECX,numa1
MOV EDX,1
INT 80H ;接受第一号(第2位)
MOV EAX,3
MOV EBX,0
MOV ECX,numa2
MOV EDX,2
INT 80H ;接受第二个数字(第1位)
MOV EAX,3
MOV EBX,0
MOV ECX,numb1
MOV EDX,1
INT 80H ;接受第二个数字(第2位)
MOV EAX,3
MOV EBX,0
MOV ECX,numb2
MOV EDX,2
INT 80H ;字符数转换
子字节[numa1],30H
子字节[numa2],30H
子字节[numb1],30H
子字节[numb2],30H
子字节[套利],30H ;;;;;;;;;;;;;;;;;;;;;;;;;; ;加个位数
MOV人,[numa2]
添加字节[numb2],人
添加字节[numb2],30H ;获得个位数的总和进
MOV AX,[numb2]
MOV字节[套利] 10
DIV字节[套利] MOV EAX,4
MOV EBX,1
MOV ECX,随身携带
MOV EDX,1
INT 80H
MOV EAX,1
MOV EBX,0
INT 80H
进行
numa1 numa2
+ numb2 numb2
---------------
numb2其中,numb2 = numb2 10%
携带= numb2 / 10
首先, XOR EBX,EBX
将会比 MOV EBX更短,速度更快,0
另外,在第1(9 + 9 = 18)的最大进位加2 1位数字的结果,所以没有必要来划分,只由10减去数量足够
MOV AX,[numb2]
MOV字节[套利] 10
DIV字节[套利]
将比以下慢得多的
MOV AX,[numb2]
子斧,10
但毕竟,为什么做这样的复杂的方式,而86已经 DAA和说明许多其他BCD数学。此外,这些说明有AF和CF适合随身携带所以没有必要去管理你自己的。你也可以直接使用二进制数学,只是将它们转换为输入/输出。大多数的情况下的转换的成本是微不足道的
I'm trying to add 2 two-digit numbers which are bound to yield a two-digit or three-digit number.
Here's what I have so far, and when I try to print the carry, it says Floating Point Exception (Core Dumped)
section .data
msg db "Enter 2 numbers: "
msgLen equ $-msg
section .bss
numa1 resb 1
numa2 resb 1
numb1 resb 1
numb2 resb 1
carry resb 1
section .text
global _start
_start:
;print message
mov eax, 4
mov ebx, 1
mov ecx, msg
mov edx, msgLen
int 80h
;accept first number (1st digit)
mov eax, 3
mov ebx, 0
mov ecx, numa1
mov edx, 1
int 80h
;accept first number (2nd digit)
mov eax, 3
mov ebx, 0
mov ecx, numa2
mov edx, 2
int 80h
;accept second number (1st digit)
mov eax, 3
mov ebx, 0
mov ecx, numb1
mov edx, 1
int 80h
;accept second number (2nd digit)
mov eax, 3
mov ebx, 0
mov ecx, numb2
mov edx, 2
int 80h
;character to number conversion
sub byte[numa1], 30h
sub byte[numa2], 30h
sub byte[numb1], 30h
sub byte[numb2], 30h
sub byte[carry], 30h
;;;;;;;;;;;;;;;;;;;;;;;;;;
;add ones digit
mov al, [numa2]
add byte[numb2], al
add byte[numb2], 30h
;get carry of sum of ones digit
mov ax, [numb2]
mov byte[carry], 10
div byte[carry]
mov eax, 4
mov ebx, 1
mov ecx, carry
mov edx, 1
int 80h
mov eax, 1
mov ebx, 0
int 80h
carry
numa1 numa2
+ numb2 numb2
---------------
numb2
where numb2 = numb2 % 10
carry = numb2 / 10
First, xor ebx, ebx
would be shorter and faster than mov ebx, 0
Also, adding 2 1-digit numbers results in a maximum carry of 1 (9 + 9 = 18), so no need to divide, just subtracting the number by 10 is enough
mov ax, [numb2]
mov byte[carry], 10
div byte[carry]
will be much slower than the following
mov ax, [numb2]
sub ax, 10
But after all, why do such "complex" way while x86 already has DAA and instructions for many other BCD maths. Besides, those instructions have AF and CF for carry so no need to manage it on your own. You can also just use binary math directly and just convert them at input/output. Most of the case the cost of converting is negligible
这篇关于DIV组装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!