1

Read a three digit number from keyboard and display the number on the screen.

readnum macro num   ; defining macro to read a three digit number
    mov ah, 01h     ; to read third (from right to left) digit 
    int 21h 
    sub al, '0'    
    mov bh, 064h    ; storing 100 or 064h into bh
    mul bh          ; here multiplier is in bh and multiplicand is in al  and product is in ax (WORD)
    mov ah, 00h
    mov num, ax     ; moving the content of al to memory location num 
    mov ah, 01h     ; to read second digit
    int 21h
    sub al, '0'    
    mov bh, 0ah
    mul bh 
    mov ah, 00h
    add num, ax
    mov ah, 01h
    int 21h
    sub al, '0'
    mov ah, 00h
    add num, ax
endm 

printstring macro msg
    mov ah, 09h
    mov dx, offset msg
    int 21h
endm


_DATA segment
    cr equ 0dh
    lf equ 0ah
    msg1 db 'Enter a three digit number: ','$'
    msg2 db cr, lf, 'You have entered: ','$'
    num dw ? 
    temp db ?
    result db 20 dup('$')
    _DATA ends 


_CODE segment
    assume cs:_CODE, ds:_DATA
    
    start: mov ax, _DATA
    mov ds, ax 
    printstring msg1
    readnum num 
    printstring msg2 
     
    printstring result
    
    mov ah, 4ch
    mov al, 00h
    int 21h
    _CODE ends
end start

What I would like to see is:

Enter a three digit number: 345
You have entered: 345

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
  • 1
    Please format your code block(s), in order to make your question more readable, and receive better quality answers. For more information, take a look on this link: https://meta.stackoverflow.com/questions/251361/how-do-i-format-my-code-blocks – Mario Mateaș Feb 15 '23 at 04:34

1 Answers1

1

The question about "How to display the multidigit inputted number in emu8086?" is answered in Displaying numbers with DOS. This alone however will not solve your trouble with the task!

Your readnum macro currently contains the mov ah, 00h instruction 3 times:

mov ah, 01h    ; Hundreds digit 
int 21h 
sub al, '0'    
mov bh, 064h
mul bh
mov ah, 00h    <<< Plain wrong, harmfull
mov num, ax 
mov ah, 01h    ; Tens digit
int 21h
sub al, '0'    
mov bh, 0ah
mul bh 
mov ah, 00h    <<< Redundant, but harmless
add num, ax
mov ah, 01h    ; Ones digit
int 21h
sub al, '0'
mov ah, 00h    <<< Necessary, so fine
add num, ax
  • If the hundreds digit happens to be 3 or more (up to 9), the product will not fit in the AL register anymore (300, 400, ...). The mov ah, 00h instruction will be destroying part of the number. Remove this instruction.
  • The multiplication of the tens digit by 10 will never produce anything larger than 90, so the value in AH will already be 0. Therefore executing mov ah, 00h is a redundant operation and you should remove it from the program.
  • For the ones digit, clearing AH is necessary so you can keep the mov ah, 00h instruction or just replace it by the 1 byte shorter cbw instruction that will clear AH in this case.
printstring macro msg
 mov ah, 09h
 mov dx, offset msg
 int 21h
endm

Because the DOS.PrintString function 09h expects a pointer to a string of text characters, the thing you need to do is convert the value contained in the num variable into such a string of characters. The answer at Displaying numbers with DOS explains this in great detail (be sure to read it), but here I have included a code that displays your num in a slightly different way. You'll learn the most if you study the differences between the several solutions that I provided.

  mov  cx, 10              ; CONST divider
  lea  bx, [result + 5]    ; Never more than 5 digits in a 16-bit value
  mov  byte ptr [bx], "$"  ; Mandatory terminator for DOS.PrintString
  mov  ax, num             ; Value to be displayed
more:
  xor  dx, dx              ; Word-sized division divides DX:AX
  div  cx                  ; -> AX is quotient, DX is remainder
  add  dl, "0"             ; Convert [0,9] into ["0","9"]
  dec  bx
  mov  [bx], dl
  test ax, ax
  jnz  more
  mov  dx, bx              ; Address of string of characters
  mov  ah, 09h             ; DOS.PrintString
  int  21h
Sep Roland
  • 33,889
  • 7
  • 43
  • 76