将两个两位数字相乘,两个数字作为输入,结果也将打印在Tasm中

时间:2013-10-04 09:48:48

标签: assembly x86

我不了解在这种情况下可以做些什么 使用AAM指令可以实现单位数乘法 但是对于AAM,你需要解压缩BCD,因此两位数乘法后的结果不会累积在AX寄存器中......

所以我需要知道如何处理这个问题。谢谢你

这里是输入应该是什么样子(取一个两位数字)和BCD是可取的

mov dx,offset msg
mov ah,09h
int 21h

mov ah,01h
int 21h

mov ch,al
sub ch,30h
ror ch,04h

mov ah,01h
int 21h

mov cl,al
sub cl,30h
add cl,ch

2 个答案:

答案 0 :(得分:1)

mynumber1 db ?
mynumber2 db ?

mov ah,01h
int 21h
sub al, 30h <- ASCII to value 

mov bl, 0Ah 
mul bl <- multiply al with 10

mov mynumber1, al <- mynumber1 now stores the tens (i.e. if you entered 8 it's now 80)

mov ah,01h
int 21h 
sub al, 30h <- ASCII to value, al now stores the ones

add mynumber1, al <- now your two-digit number is completely in mynumber1

现在对mynumber2重复相同的操作。然后:

mov al, mynumber1
mov bl, mynumber2

mul bl

现在产品在AX。如果您确实需要,请将AX的内容转换回BCD。


以下代码将打印存储在AX中最多4位的数字:

xor dx,dx
mov bx,03E8h
div bx
call printdig

mov ax,dx
xor dx,dx
mov bx,0064h
div bx
call printdig

mov ax,dx
xor dx,dx
mov bx,000Ah
div bx
call printdig

;remainder from last div still in dx
mov al,dl
call printdig

请注意,您需要以下辅助函数,该函数从al打印一个数字:

printdig proc
push dx
mov dl,al
add dl,30h
mov ah,02h
int 21h
pop dx
ret
printdig endp

答案 1 :(得分:0)

汇编语言中两位数的乘法,使用 BCD 指令(如 aam (ASCII adjust AX After Multiply))处理十进制数,每字节一位十进制数。

(另一种方法是将 2 位输入分别转换为一个字节的二进制整数,并执行标准的 mul,然后将 AX 中的结果转换回字符串,如 {{3 }})

org 100h

.data      
num1_O db ?
num2_O db ?
num1_T db ?
num2_T db ? 
temp db ? 
carry db ? 
1st db ?
2nd db ?
3rd db ?
4th db ?
ent_num db " ENTER NUMBER $"
ans db "mul of both number is = $"

.code
start:                          
     mov ax, @data
     mov ds, ax
     mov dx, offset ent_num
     mov ah, 09h
     int 21h
     
     mov ah, 01h               ;1
     int 21h   
     sub al, 30h
     mov num1_T, al
     
     mov ah, 01h               ;2
     int 21h   
     sub al, 30h
     mov num1_O, al  
     
     
     mov dx, offset ent_num
     mov ah, 09h
     int 21h  
     
     mov ah, 01h                 ;1
     int 21h
     sub al, 30h
     mov num2_T, al
     
     mov ah, 01h                  ;2       al
     int 21h
     sub al, 30h 
     mov num2_O, al 
     
     mov 1st, 0
     mov 2nd, 0
     mov 3rd, 0
     mov 4th, 0
     
     
     
     mov al, num2_O
     mul num1_O
     mov ah, 00h
     aam
     
     add 3rd, ah         ;1st carry            4th solved
     add 4th, al 
     
     
     mov al, num2_O
     mul num1_T
     mov ah, 00h
     aam
     
     add 2nd, ah            ;carry
     add 3rd, al  
                    
     
     mov al, num2_T
     mul num1_O
     mov ah, 00h
     aam
     
     add 2nd, ah            ;carry
     add 3rd, al   
     
     
     mov al, num2_T
     mul num1_T
     mov ah, 00h
     aam
     
     add 1st, ah            ;carry
     add 2nd, al   
     
     mov dl, 010
     mov ah, 02h
     int 21h
     mov dx, offset ans
     mov ah, 09h
     int 21h
     
     
     mov al, 3rd
     mov ah, 00h
     aam
     
     add 2nd, ah
     mov 3rd, al 
     
     
     mov al, 2nd
     mov ah, 00h
     aam
     
     mov 2nd, al
     add 1st, ah
     
     mov dl, 1st
     add dl, 30h
     mov ah, 02h
     int 21h 
     
     mov dl, 2nd
     add dl, 30h
     mov ah, 02h
     int 21h
     
     mov dl, 3rd
     add dl, 30h
     mov ah, 02h
     int 21h
     
     mov dl, 4th
     add dl, 30h
     mov ah, 02h
     int 21h
     end start
   

ret