Search code examples
assemblydosx86-16

How to print a BYTE size number in 8086 assembly


Here is how to print numbers in word size. I can't make it work for byte size numbers.

.model  small
.stack   100h
.data
num  dw  300   ; <----- See below
numS db  6 dup(' '),'$'**

.code
    mov ax, @data
    mov ds, ax
    mov ax, num
    mov bx, 10         
    mov si, offset numS+5
    
    cmp ax,0
    jge next
    neg ax
 next:  
    mov dx,0
    div bx
    add dl, 48
    mov [si],   dl
    dec si
    cmp ax, 0
    jne next
    
    cmp num,0
    jge sof
    mov byte ptr[si],   '-'
    dec si
    
sof:
    inc si
    mov ah, 9
    mov dx, si
    int 21h
    .exit
end

I changed num dw to num db but something is wrong in the code I tried to change bx ..
Can anyone change operand types to match because the program says it's the problem. I need this code to not use other methods like pop, push, ret.


Solution

  • The good news is that you can keep using the conversion that you have. If you change the line num dw 300 into something like num db 36, all you have to modify in the code is how you load the AX register. Instead of mov ax, num, you now write mov al, num cbw. (You're expecting signed numbers apparently!)


    An all bytes solution is possible though:

        mov ax, @data
        mov ds, ax
        mov al, num
        mov bl, 10         
        mov si, offset numS+5
        
        cmp al,0
        jge next
        neg al
     next:  
        mov ah, 0
        div bl        ; Calculates AX / BL --> AL is quotient, AH is remainder
        add ah, 48
        dec si
        mov [si], ah
        cmp al, 0
        jne next
        
        cmp num, al      ; AL == 0
        jge sof
        dec si
        mov byte ptr [si], '-'
    sof:
        mov dx, si
        mov ah, 09h      ; DOS.PrintString
        int 21h