TASM string to hexadecimal number

1.2k Views Asked by At

I have a problem with a TASM based assembly code. I would like to convert a string in the "string" pointer into a decimal number and then print it as a hexadecimal one. However the code prints only the first 2 characters from the right correctly. What may be wrong with this?

concd   SEGMENT
        ASSUME cs: concd
        ORG 100h

main:   
    mov ax, 0
    mov bx, offset string
    mov cx, 0
    mov dx, 0       
    mov si, 0       

jump:   
    mov cx, [bx + si]
    cmp cx, 0h          ;checking if we reached the end of the string
    jz exit

    mov dx, ax
    mov cx, 9

mult:   
    add ax, dx          ;loop for multiplying by 10 
    loop mult

    mov cx, [bx + si]
    sub cx, 30h
    add ax, cx      

    inc si      
    jmp jump

exit:   
    call hex
    mov ah, 04Ch
    mov al, 0
    int 21h

hex PROC
    mov dx, ax
    mov cl, 12
jump2: 
    mov ax, dx
    shr ax, cl
    and ax, 15
    cmp ax, 9
    jng t
    add ax, 7h
t:  add ax, 30h
    call putc
    sub cl, 4
    jnc jump2

    RET
hex ENDP

putc PROC

    mov ah, 0Eh
    int 10h

    RET
putc ENDP

string  DB  "1234", 0h

concd   ENDS
    END     main
1

There are 1 best solutions below

0
On

I made some small changes, and now it works. I commented with "phase 1" the changes that it actually did get a character abd did something, and with "phase 2" the changes that corrected the conversion.

Now it's OP's time to figure out what was wrong.

concd   SEGMENT
        ASSUME cs: concd
        ORG 100h

main:   
    mov ax, cs ;; added - phase 1
    mov ds, ax ;; added - phase 1
    mov ax, 0
    mov bx, offset string
    mov cx, 0
    mov dx, 0       
    mov si, 0       

jump:   
    mov ch, 0  ;; Added - phase 2
    mov cl, [bx + si] ;; added - phase 2
    ;mov cx, [bx + si] ;; removed - phase 2
    cmp cx, 0h          ;checking if we reached the end of the string
    jz exit

    mov dx, ax
    mov cx, 9

mult: ; ax contains the number
    ;; removed the next 2 lines - phase 2
    ;add ax, dx          ;loop for multiplying by 10 
    ;loop mult
    ;; added the next 2 lines - phase 2
    mov cl, 10
    mul cl          ; multiply by 10

    mov cx, [bx + si]
    sub cx, 30h
    add ax, cx      

    inc si      
    jmp jump

exit:   
    call hex
    mov ah, 04Ch
    mov al, 0
    int 21h

hex PROC
    mov dx, ax
    mov cl, 12
jump2: 
    mov ax, dx
    shr ax, cl
    and ax, 15
    cmp ax, 9
    jng t
    add ax, 7h
t:  add ax, 30h
    call putc
    sub cl, 4
    jnc jump2

    RET
hex ENDP

putc PROC

    mov ah, 0Eh
    int 10h

    RET
putc ENDP

string  DB  "1234", 0h

concd   ENDS
    END     main