Hi! I need help for multiplication. We have to multiply 2 3-digit hex numbers.
I have made this last year but this one is a multiplication using 2-digit decimal numbers.
Hope you guys can help me with this one. :s
I have made this last year but this one is a multiplication using 2-digit decimal numbers.
Hope you guys can help me with this one. :s
;
;2-digit MULTIPLICATION (in decimal)
jmp start
clrscr:
mov ax,3
int 10h
ret
input_char:
mov ah,1
int 21h
ret
print_char:
mov ah,2
int 21h
ret
print_str:
mov ah,9
int 21h
ret
check:
cmp al,'0'
jb mali
cmp al,'9'
ja mali
ret
mali:
lea dx,msg4
call print_str
jmp tapos
start:
call clrscr
;input 1st number
lea dx,msg1
call print_str
call input_char
call check
sub al,30h
mov bl,al
mov al,10d
mul bl
push ax
call input_char
call check
sub al,30h
mov bl,al
pop ax
add al,bl
push ax
;input 2nd number
lea dx,msg2
call print_str
call input_char
call check
sub al,30h
mov bl,al
mov al,10d
mul bl
push ax
call input_char
call check
sub al,30h
mov bl,al
pop ax
add al,bl
push ax
pop ax
mov bl,al
pop ax
mul bx ;multiplication of the 2 numbers (stored in ax)
mov cx,000ah
div cx ;ones digit(remainder) in dl
push dx ;to stack
mov dl,00h ;reset dl
div cx ;tens digit in dl
push dx ;to stack
div cl ;thousands digit in al / hundreds digit in ah
or ax,3030h ;convert to printable characters
push ax ;to stack
lea dx,msg3
call print_str
;print thousands digit
mov dl,al
call print_char
;print hundreds digit
pop ax
mov dl,ah
call print_char
;print tens digit
pop dx
or dl,30h
call print_char
;print ones digit
pop dx
or dl,30h
call print_char
tapos:
int 20h
msg1 db "Enter 1st number[2-digit DECIMAL(00-99)]: $"
msg2 db 0ah,0dh,"Enter 2nd number[2-digit DECIMAL(00-99)]: $"
msg3 db 0ah,0dh,"Their PRODUCT is: $"
msg4 db 0ah,0dh,"Input is not in DECIMAL. Please try again.$"
Multiplying two numbers is going to be just the same, whether they're "hex" or "decimal" - these are just two ways to represent the number! What's going to need to change is the way you convert the characters the user inputs into a number. Start by thinking about your "check" routine. You're going to want to allow 'a'..'f' or 'A'..'F', along with '0'..'9'.
In your current code, when "check" returns (if it does), you subtract '0' (30h). Since you're going to want to subtract a different amount if it's a hex digit, you might want to incorporate this into a "check_hex_and_convert" routine, or something...
Then you multiply by ten (if needed). To do hex, you'd want to multiply by 16. This can more easily(?) be accomplished by shifting left 4 bits... You do the two digits "in line", one after the other. You could do this with three digits, or maybe you'd want to do it in a loop. Make sure you've got a "next digit", multiply (or shift) the "result so far", and add (or "or") in the new digit... repeat until done...
Once you've got the two numbers, multiply "as usual". Now I suppose you'll want to print the result in hex, too(?). Same idea as above, only "backwards". Instead of "div", you can use "rol reg, 4" to get the "first" (or "next") digit into a convenient position, Make a copy of it(!), and mask off the low bits with "and reg, 0Fh" - that's your digit, add '0'... if it's over '9', add another 7 to get it into the 'A..'F' range. Easier than decimal, in a way, since we can get the digits in the right order to print 'em! :)
Getting the number from the user is probably the "hard part", since you need to check for both upper and lower case - or you could insist that the user stick to one case or the other. Or you could force the case yourself... really ought to make sure it's an "alpha" character, that *has* a case, but if it isn't, it's "invalid" anyway... "and reg, 0DFh" would force to upper...
Give it a shot - get one part working before you attempt the next part. If/when you run into trouble, ask again! :)
Best,
Frank
In your current code, when "check" returns (if it does), you subtract '0' (30h). Since you're going to want to subtract a different amount if it's a hex digit, you might want to incorporate this into a "check_hex_and_convert" routine, or something...
Then you multiply by ten (if needed). To do hex, you'd want to multiply by 16. This can more easily(?) be accomplished by shifting left 4 bits... You do the two digits "in line", one after the other. You could do this with three digits, or maybe you'd want to do it in a loop. Make sure you've got a "next digit", multiply (or shift) the "result so far", and add (or "or") in the new digit... repeat until done...
Once you've got the two numbers, multiply "as usual". Now I suppose you'll want to print the result in hex, too(?). Same idea as above, only "backwards". Instead of "div", you can use "rol reg, 4" to get the "first" (or "next") digit into a convenient position, Make a copy of it(!), and mask off the low bits with "and reg, 0Fh" - that's your digit, add '0'... if it's over '9', add another 7 to get it into the 'A..'F' range. Easier than decimal, in a way, since we can get the digits in the right order to print 'em! :)
Getting the number from the user is probably the "hard part", since you need to check for both upper and lower case - or you could insist that the user stick to one case or the other. Or you could force the case yourself... really ought to make sure it's an "alpha" character, that *has* a case, but if it isn't, it's "invalid" anyway... "and reg, 0DFh" would force to upper...
Give it a shot - get one part working before you attempt the next part. If/when you run into trouble, ask again! :)
Best,
Frank