1

i am not understanding as to what can be done in this case single digit multiplication was possible using the AAM instruction however for AAM you need unpacked BCD therefore the result after two digit multiplication wont be accumulated in the AX register...

so i need an idea as to how i can proceed with this problem .thank you

here is how the input should look like (to take one two digit number) and BCD is desirable

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
Denson
  • 121
  • 1
  • 2
  • 11
  • 3
    This question is currently very unclear. What does your input look like? What is the output supposed to look like? Is it a requirement to use BCD? Post the relevant parts of your code. – Michael Oct 04 '13 at 09:59
  • @Denson Since you are reading the input from the keyboard, you are not bound to an input format since you need to convert from ASCII anyways. In my opinion, there is therefore no advantage to doing the calculations in BCD. Do the calculations in standard binary and THEN convert the result to BCD if you really need to. – us2012 Oct 04 '13 at 12:25
  • can u elaborate on your solution if possible ? – Denson Oct 04 '13 at 12:33
  • The solution is very simple. Read from console with `int 21h`, convert from ASCII to an actual number (decide on how much error handling you want and if you include negative numbers), multiply with `MUL` or `IMUL`, convert back to ASCII, write to console with `int 21h`. That's really all there is to it. – Daniel Kamil Kozar Oct 04 '13 at 13:31
  • i dont think that works for two digit numbers @DanielKamilKozar but if possible make yourself more clear – Denson Oct 04 '13 at 13:50
  • @Denson It doesn't work easily for BCD, that's why we're all saying you shouldn't use BCD. – us2012 Oct 04 '13 at 13:54
  • "BCD is desirable" - why? – lurker Oct 04 '13 at 14:49
  • we were instructed to use that @mbratch but it is okay to use anything as long as i get the answer PS: my instructors aren't that good unfortunately – Denson Oct 04 '13 at 14:56

2 Answers2

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

Now repeat the same for mynumber2. Then:

mov al, mynumber1
mov bl, mynumber2

mul bl

Now the product is in AX. Proceed by converting the content of AX back to BCD, if you really need to.


The following code will print a number with up to 4 digits stored in AX:

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

Note that you need the following helper function, which prints a single digit from al:

printdig proc
push dx
mov dl,al
add dl,30h
mov ah,02h
int 21h
pop dx
ret
printdig endp
us2012
  • 16,083
  • 3
  • 46
  • 62
  • can u tell me how am i supposed to output the number from ax without converting to BCD the way i output the number doesn't work in this case @us2012 – Denson Oct 04 '13 at 14:31
  • @Denson See my answer to the following question: http://stackoverflow.com/questions/19147472 . It explains an easy algorithm for printing numbers. – us2012 Oct 04 '13 at 14:36
  • i went through your algorithm suppose i have a four digit number in ax how do i go about that? will the algorithm work for this too? – Denson Oct 04 '13 at 15:11
  • mov prod,ax mov bx,ax mov cx,ax mov dx,ax mov bl,0ah mov ch,00h mov dl,00h ror dx,08h mov ax,cx div bl mov temp1,al mov temp2,ah add temp1,30h add temp2,30h mov ax,dx div bl mov temp3,al mov temp4,ah add temp3,30h add temp4,30h this is the code i wrote to output the number(assuming i used ur algo properly) it gives incorrect output – Denson Oct 04 '13 at 16:03
  • That's really hard to read but just from seeing `ror dx, 08h` and the fact that there's not a single constant `0Ah` in your code I can tell you that you aren't using it correctly at all. Remember that you are NOT dealing with BCDs. I may have some time later to expand on my answer... – us2012 Oct 04 '13 at 16:40
  • mov bl,0ah i used 0Ah ya i think am used to visualising data as BCD!! – Denson Oct 04 '13 at 16:45
  • i have some questions regarding your code why are you pushing dx in the stack? and why are u doing this operation? (mov bx,03E8h) and thanks a ton for your help! @us2012 – Denson Oct 05 '13 at 06:12
  • @Denson I'm pushing an popping DX because it holds the remainder of the division, and I need to save that across the printing process (but the printing process needs to use DX through DL!). `03E8h` is `1000`, so this is for the division by 1000 to get the thousands digit. If this answer helped you, please consider accepting it: http://meta.stackexchange.com/questions/5234 – us2012 Oct 05 '13 at 09:45
  • the answer to the multiplication – Denson Oct 05 '13 at 11:40
  • Have you *checked* that with a debugger? Also check whether the register contents match your expectations before the exception is thrown. I have tested this code quite a bit in dosbox, so I'm fairly sure it is correct. – us2012 Oct 05 '13 at 11:44
  • I have used your code for multiplication and printing as well. – Denson Oct 05 '13 at 12:04
  • Well, the only possibilities that I can still see is that either you're not reading `number2` correctly or the way you integrated the `printdig` function somehow doesn't work. If neither of that is the case, I can't help you. In that case, *use a debugger*. I have given you a nearly complete solution for your homework problem, which is more than I should really. I'm also fairly certain that it's correct but even if there still is some mistake in there you can find it with the help of a debugger. – us2012 Oct 05 '13 at 12:15
  • mov bl, 0Ah then mul bl then you saved it in a byte variable shouldnt it be saved in a word variable? this can be the possible mistake and ya i do acknowlege what you have done – Denson Oct 05 '13 at 12:26
  • Not really. You are right that the result of the multiplication is in AX, and hence a word, but we know that it's only two decimal digits - so less than 100, which is less than 255 (0FFh), and therefore we only need to save the lower byte, AL. – us2012 Oct 05 '13 at 12:28
  • oh ya i used the debugger it popps out a illegal instruction error while running then how does it compile and link in the first place! i am getting confused now! – Denson Oct 05 '13 at 12:33
0

Multiplication of 2 digit number in assembly language, using BCD instructions like aam (ASCII adjust AX After Multiply) to work with the numbers in decimal, one decimal digit per byte.

(The other way would be to convert the 2-digit inputs to a one-byte binary integer each, and do a standard mul, then convert the result in AX back to a string like in Displaying numbers with DOS)

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
Peter Cordes
  • 328,167
  • 45
  • 605
  • 847