Search code examples
assemblyemu8086

Write instructions to load 26 (decimal) into register cx


I'm having problem loading 26 and other 2-digits decimal numbers into registers.

I know that '0' has an ASCII value of 48 and I need to add 48 to any number from 0 to 9 to get the ASCII values, but I don't know how to load 2-digit numbers.

.model small
.data
.code
main proc


    mov dl, 2



    add dl, 48 ; this makes the character ascii

    ;code for printing a character
    mov ah, 2h
    int 21h ; prints value of dl
endp
end main

...


Solution

  • loading 26 and other 2-digits decimal numbers into registers

    This is the easy part. All the 2-digit decimal numbers are within the range [10,99].
    To load those into a register like CX, you simply write

    mov cx, 10
    mov cx, 11
    ...
    

    What your program is doing is something completely different. You're trying to display such a 2-digit decimal number. This requires decomposing the number into its 2 characters. You do this through dividing the number by 10. The quotient is the first digit to print, the remainder is the second digit to print.

    mov     ax, cx     ; Division exclusively works with AX
    mov     dl, 10     ; Divisor
    div     dl         ; AX / DL -> Quotient in AL, Remainder in AH
    add     ax, 3030h  ; Make both ASCII at the same time
    mov     dx, ax     ; DL holds "quotient"-character, DH holds "remainder"-character
    mov     ah, 02h    ; DOS.DisplayCharacter
    int     21h
    mov     dl, dh     ; Bring "remainder"-character in DL
    mov     ah, 02h    ; DOS.DisplayCharacter
    int     21h