Search code examples
assemblydosx86-16tasm

Creating a simple addition calculation but error when compiling [Assembly Code]


I am currently learning assembler language. It not easy and I am still learning but I wanted to create a simple addition calculation by getting the value from the user but I couldn't even get the compiler to run my code it keeps on saying illegal command:

enter image description here File name is USERSUM.ASM

I am using the DOSbox to write mine assembler code using the TASM file

title Calculate 2 Sum
; This program is to calculate the sum input from the user

.model small
.stack 100h

.data
input1 db 0ah, odh, "Input 1st Number: ", "$"
input2 db 0ah, odh, "Input 2nd Number: ", "$"
output db 0ah, odh, "The sum is: ", "$"

number 1 dw ?
number 2 dw ?
result dw ?

.code
main  proc

      MOV ax, @data
      MOV ds,ax

;Print 1st Message Input
      MOV ah, 9
      MOV dx,offset input1
      int 21h

      MOV ah, 1
      int 21h
      MOV bl, al

;Print 2nd Message Input
      MOV ah, 9
      MOV dx,offset input2
      int 21h

      MOV ah, 1
      int 21h
      MOV bh, al

;addition
      SUB number1,'0'
      SUB number2,'0'

      MOV ax, number1
      add ax, number2

      MOV result, ax
      add result,'0'

;Print Output 
      MOV ah, 9
      MOV dx,offset output
      int 21h

      MOV ah, 2
      MOV dl, bh
      int 21h

      MOV ax, 4C00h
      int 21h

main  endp
end   main

I want my output to be like this:-

Input 1st Number: 2
Input 2nd Number: 4
The sum is: 6

I can't figure out the problem. Am I missing something in the code or did I forget to put something important in? Can anybody tell me what I did wrong?


Solution

  • This is because the register dl is a byte but your variableresult is defined as a word by dw ,so the assembler says operand is not same

    just use the ptr operator to change the type of result to byte like this

    MOV dl, byte ptr result
    

    or just use dx

    mov dx,result
    

    this is my code modified from the code in your answer,note it is 0dh instead of odh

    .model small
    .stack 100h
        
    .data
    input1 db 0ah, 0dh, "Input 1st Number: ", "$";NOTE here and the next 2 lines is 0dh instead of odh
    input2 db 0ah, 0dh, "Input 2nd Number: ", "$"
    output db 0ah, 0dh, "The sum is: ", "$"
    
    number1 dw ?
    number2 dw ?
    result dw ?
    
    .code
    main  proc
    
          MOV ax, @data
          MOV ds,ax
    
    ;Print 1st Message Input
          MOV ah, 9
          MOV dx,offset input1
          int 21h
    
          MOV ah, 1
          int 21h
          MOV number1, ax
    
    ;Print 2nd Message Input
          MOV ah, 9
          MOV dx,offset input2
          int 21h
    
          MOV ah, 1
          int 21h
          MOV number2, ax
    
    ;addition
          SUB number1,'0'
          SUB number2,'0'
    
          MOV ax, number1
          add ax, number2
    
          MOV result, ax
          add result,'0'
    
    ;Print Output
          MOV ah, 9
          MOV dx,offset output
          int 21h
    
          MOV ah, 2
          MOV dx,  result;or use mov dl,byte ptr result
          int 21h
    
          MOV ax, 4C00h
          int 21h
    
    main  endp
    end   main
    

    output of the code