Victor Alagwu
Victor Alagwu

Reputation: 87

What is wrong with my assembly language code

I have a problem with this code, it is meant to collect a number and double it, but it only accept numbers between 1 and 9, and output the second value for all double of numbers between 5 and 9, what can be done to allow it output the right value

;DOUBLE THIS PROGRAM PROMPTS THE USER TO ENTER A NUMBER
;DOUBLE THE NUMBER,AMD OUTPUT THE RESULT
.model small
.stack
.data
prompt db 0ah,0dh,  'enter the number : $'
msg db 0ah,0dh,  'Double your number is : '
result db 0ah,0dh, '$'
.code
start:
      mov ax,@data
      mov ds,ax


      lea dx,prompt             
      mov ah, 9               ;dos fn to output string up to $
      int 21h
      mov ah,1                ;dos fn to input byte into al
      int 21h
      sub al,30h                ;convert from ascii to integer
      add al,al                 ;sum inputted value to itself
      aaa
      or al,30h

      mov result,al             ;add the double value of the value to result



      lea dx, msg 

      mov ah, 9
      int 21h


      mov ah,4ch
      int 21h            

      end start

Upvotes: 1

Views: 1036

Answers (1)

Fifoernik
Fifoernik

Reputation: 9899

You got a few problems in your program:

  • You didn't reserve the necessary space to store the byte sized result. You need an extra zero after the db directive.

    result db 0, 0ah, 0dh, '$'
    
  • Because doubling the numbers from 5 to 9 will inevitably yield a 2-digit result, you simply have to change the result to word size.

    result db 0, 0, 0ah, 0dh, '$'
    
  • You are using the aaa instruction wrong. Luckily there is the aam instruction that suits your purpose.

    AAM divides AL by ten and stores the quotient in AH, leaving the remainder in AL.

    The next code will always display the result with 2 digits. If desired, you could replace the character "0" in AL with a " " just before writing AX in the result variable.

    aam
    or   ax, 3030h
    xchg al, ah
    mov  result, ax
    

Upvotes: 2

Related Questions