Page 1 of 1

# [TASM] Decimal input --> hex output

Posted 23 September 2018 - 03:21 PM

Hello everyone. I'm very new to assembler and I find it very difficult to get around the idea of it. I have an assignment to convert decimal digit from 0 to 65535 into hex. The problem is that my program won't print 4 digit hexadecimal numbers. For example, it will print 5FF instead of FFFF or 711 instead of 1111 and i can't find the mistake. Please help.
```.model small

bufSize EQU 121

.stack 100h

.data

bufSize DB  bufSize
read DB  ?              ;how many symbols
buf  DB  bufSize dup (?)

msg  DB  'Please, enter decimal digit: \$'
enterr  DB  13, 10, '\$'
errormsg DB  'wrong input \$'

.code

begin:
MOV ax, @data
MOV ds, ax

MOV ah, 9
MOV dx, offset msg
INT 21h

MOV ah, 0Ah
MOV dx, offset bufSize
INT 21h

MOV ah, 9
MOV dx, offset enterr
INT 21h

;****algorythm****
XOR ax, ax
MOV cl, read        ;how many symbols

MOV bx, offset buf  ;first symbol to bx

check:
CMP cl, 0
MOV ah, 9
JNE loop0
MOV dx, offset errormsg
INT 21h         ;print result
JE ending

errorr:
MOV ah, 9
MOV dx, offset errormsg
INT 21h         ;print result
JMP ending
loop0:
PUSH ax
PUSH dx
XOR ax,ax
XOR dx,dx
JMP loop1

loop1:
MOV dh,0Ah
MUL dh
MOV dl,[bx]
cmp dl, '0'
JB errorr
cmp dl, '9'
JA errorr

SUB dl,30h

loop2:
INC bx

DEC cl
CMP cl, 0
JNE loop1

MOV cx, 16
PUSH '\$\$'

Division:
MOV dx, 0
DIV cx      ;[DX,AX]:10 = AX(remainder DX)
PUSH dx
CMP ax, 0
JA  Division

;printing
MOV ah, 9
MOV dx, offset form
INT 21h
MOV ah, 2
Print:
POP dx
CMP dx, "\$\$"
JE  ending
CMP dx, 9
JNBE above
less:
INT 21h
JMP Print
above:
INT 21h
JMP Print

pop dx
pop ax

ending:
MOV ah, 4Ch
MOV al, 0
INT 21h

END begin

```

Is This A Good Question/Topic? 0

## Replies To: [TASM] Decimal input --> hex output

### #2 Programmer2004 Reputation: 18
• Posts: 96
• Joined: 25-October 17

## Re: [TASM] Decimal input --> hex output

Posted 24 September 2018 - 07:16 AM

I think that I found the cause of your problem.

The problem is in loop1, you multiply AL by DH, which overwrites the AH, so this algorithm completely ignores the upper 8 bits. You need to multiply AX by DX.

And now, I'll explain why because of that bug you for example get result 5FF instead of FFFF when you type 65536:

1. First, AX is cleared to 0x0, which is of course perfectly fine
2. Multiply AL by 0xA, which results in AX being 0x0, which also produces correct result
3. Add 6, now AL is 0x6
4. Multiply AL by 0xA, AX is now 0x3C
5. Add 5, now AL is 0x41
6. Multiply AL by 0xA, AX is now 0x28A
7. Add 5, now AL is 0x8F
8. And this is where things are going wrong. Multiply AL by 0xA, which results in AX being 0x596
9. Add 3, now AL is 0x99
10. Again, multiply AL by 0xA, AX is now 0x5FA
11. Add 5, now AL is 0xFF, so this results in AX being 0x5FF

And here it ends, because there are no more digits.
So generally speaking multiplying by 8-bit value will give a 16-bit result which is stored in AX.
You need to set DX to 0xA, and multiply by DX, not by DH.

This post has been edited by Programmer2004: 24 September 2018 - 07:18 AM

## Re: [TASM] Decimal input --> hex output

Posted 27 September 2018 - 01:53 AM

Thank you! I think I got it.

### #4 Programmer2004 Reputation: 18
• Posts: 96
• Joined: 25-October 17

## Re: [TASM] Decimal input --> hex output

Posted 27 September 2018 - 01:56 AM

You're welcome.

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }