I'll trying to code Bases, I'm trying to code using bases but when I encode there is a correct equivalent value appearing but there are more incorrect equivalents. What wrong in my code?
My code: Needs to type 2 digits and based on the required base range and the equivalent base will appear. If you notice that I don't have any comparing to find out if base 3 or the required base needs to be input, I'll put it next but first I have to think about what's wrong with the logic of my code.
My Code in notepad :
.model small
.stack 100h
.data
Spc db 0dh,0ah, " $" ;New Line
;Bases Conversion
ConT db 0dh,0ah, " Conversion $" ;Conversion Title
ConBs3 db 0dh,0ah, "Base 3 to Base 5 "
db 0dh,0ah,0dh,0ah, "Base 3 [00 to 22] : $" ;Enter Base 03 Number
EqBs3 db 0dh,0ah, "Base 5 Equivalent : $" ;Equivalent Base 05
ConBs4 db 0dh,0ah, "Base 4 to Base 5 "
db 0dh,0ah,0dh,0ah, "Base 4 [00 to 33] : $" ;Enter Base 04 Number
EqBs4 db 0dh,0ah, "Base 5 Equivalent : $" ;Equivalent Base 05
ConBs5 db 0dh,0ah, "Base 5 to Base 4 "
db 0dh,0ah,0dh,0ah, "Base 5 [00 to 44] : $" ;Enter Base 05 Number
EqBs5 db 0dh,0ah, "Base 4 Equivalent : $" ;Equivalent Base 04
.code
main proc
mov ax,@data ;initialize ds
mov ds,ax
Base3:
mov ah,09h
lea dx, Spc ;new line
int 21h
lea dx, ConT
int 21h
lea dx, ConBs3
int 21h
mov ah,01h
int 21h ;1st Digit
sub al,30h
mov ch,al
mov ah,01h
int 21h ;2nd Digit
sub al,30h
mov cl,03h
mul cl
mov bx,ax
Con1:
add ch,bl
mov ax,0000h
mov al,ch
mov bh,05h
div bh
mov cx,ax
add cx,3030h
mov ah,09h
lea dx, EqBs3
int 21h
mov ah,02
mov dl,cl
int 21h
mov dl,ch
int 21h
Base4:
mov ah,09h
lea dx, Spc ;new line
int 21h
lea dx, ConBs4
int 21h
mov ah,01h
int 21h ;1st Digit
sub al,30h
mov ch,al
mov ah,01h
int 21h ;2nd Digit
sub al,30h
mov cl,04h
mul cl
mov bx,ax
Con2:
add ch,bl
mov ax,0000h
mov al,ch
mov bh,05h
div bh
mov cx,ax
add cx,3030h
mov ah,09h
lea dx, EqBs4
int 21h
mov ah,02
mov dl,cl
int 21h
mov dl,ch
int 21h
Base5:
mov ah,09h
lea dx, Spc ;new line
int 21h
lea dx, ConBs5
int 21h
mov ah,01h
int 21h ;1st Digit
sub al,30h
mov ch,al
mov ah,01h
int 21h ;2nd Digit
sub al,30h
mov cl,05h
mul cl
mov bx,ax
Con3:
add ch,bl
mov ax,0000h
mov al,ch
mov bh,04h
div bh
mov cx,ax
add cx,3030h
mov ah,09h
lea dx, EqBs5
int 21h
mov ah,02
mov dl,cl
int 21h
mov dl,ch
int 21h
mov ah,4Ch ;end here
int 21h
main endp
end main
Equivalent Output Error:
CodePudding user response:
Building the number is faulty
mov ah,01h int 21h ;1st Digit sub al,30h mov ch,al mov ah,01h int 21h ;2nd Digit sub al,30h mov cl,03h mul cl mov bx,ax
The first digit that you input it the Most Significant Digit ands that is the digit that you need to multiply by the number base. Your code is erroneously multiplying the Least Significant Digit!
mov ah, 01h
int 21h ; 1st Digit
sub al, 30h
mov cl, 3 ; Radix
mul cl
mov ch, al ; -> CH = MostSignificantDigit * Radix
mov ah, 01h
int 21h ; 2nd Digit
sub al, 30h
add ch, al ; -> CH = MostSignificantDigit * Radix LeastSignificantDigit
Same for the other number bases too.