# Prompt the user to enter two numbers to add and display the sum

Page 1 of 1

## 6 Replies - 17831 Views - Last Post: 28 January 2010 - 04:09 AM

### #1 qwert12345

Reputation: 0
• Posts: 169
• Joined: 26-October 08

# Prompt the user to enter two numbers to add and display the sum

Posted 27 January 2010 - 09:16 AM

Hi, I'm taking an assembly language course in school and I'm lost. I'm using the tasm assembler.

Program requirements:

The program must prompt the user to enter two numbers at a time. Add the two numbers together and display the sum of the two numbers.

Sample output:
First number: 5
Second number: 2
Sum: 5 + 2 = 7

here's my code:
```.model small
.stack 100h
.data
prompt db 13, 10, 'First number:','\$'
prompt db 13,10, 'Second number:', '\$'
result db 13, 10, 'Sum','\$'

;Variables

num1 db ?
num2 db ?
sum db ?

.code
main proc

mov ax,@data		   ;get data segment address
mov ds,ax			   ;initialize ds

;Display Prompt

mov ah,9	;print string function
mov dx,offset prompt;ds:dx points to string
int 21h

; Numbers from the user
mov ah,1 	;input function
int 21h
mov bl,al 	;save the value from input
mov num1,al

mov ah,9
lea dx, prompt 	;print prompt
int 21h

mov ah,2 	;input second function
int 21h
mov bh,al 	;save the value from second input
mov num2,al

mov ax,num1			;move num1 into ax
mov sum,ax			 ;move the total sum of numbers in sum

;Print Sum
mov ah,9
lea dx, result		 ; print result
int 21h

mov ah,2
mov dl,bl
int 21h

mov dl,'+'			 ;display + sign
int 21h

mov dl,bh
int 21h

mov dl,'='			 ;display = sign
int 21h

mov dl,bh
int 21h

mov ah,4ch
int 21h

main endp

end main

```

Is This A Good Question/Topic? 0

## Replies To: Prompt the user to enter two numbers to add and display the sum

### #2 Martyn.Rae

• The programming dinosaur

Reputation: 538
• Posts: 1,406
• Joined: 22-August 09

## Re: Prompt the user to enter two numbers to add and display the sum

Posted 27 January 2010 - 10:27 AM

You have not specified the problem, but I notice that you have two data labels named prompt. Rename them to promt1 and prompt2. Remember to change the code to reflect the new names as well.

### #3 qwert12345

Reputation: 0
• Posts: 169
• Joined: 26-October 08

## Re: Prompt the user to enter two numbers to add and display the sum

Posted 27 January 2010 - 10:47 AM

Sorry, in the Addition section in the code, all the errors for the code are "operand types do not match".

### #4 Martyn.Rae

• The programming dinosaur

Reputation: 538
• Posts: 1,406
• Joined: 22-August 09

## Re: Prompt the user to enter two numbers to add and display the sum

Posted 27 January 2010 - 02:00 PM

Right, you have declared the num1, num2 and sum values as db which is a single 8-bit values:

```num1 db ?
num2 db ?
sum db ?

```

When you use them in your code, you refer to them as 16-bit values:

```		mov ax,num1		 ;move num1 into ax
mov sum,ax		;move the total sum of numbers in sum

```

This is the reason for the "operand types do not match" error on each of those lines.
Either you need to declare the values as

```num1 dw ?
num2 dw ?
sum dw ?

```

or change the code to

```		mov al,num1		 ;move num1 into ax
mov sum,al		;move the total sum of numbers in sum

```

Also, remember the numbers retrieved from the user will be in ASCII. You will need to subtract ASCII '0' from both num1 and num2 before you add them together and you also need to add ASCII '0' the sum before you display it.

Consider what happens if the user does not enter a digit in the range '0' thru '9'. You do not have any tests there to check!

This post has been edited by Martyn.Rae: 27 January 2010 - 02:33 PM

### #5 qwert12345

Reputation: 0
• Posts: 169
• Joined: 26-October 08

## Re: Prompt the user to enter two numbers to add and display the sum

Posted 27 January 2010 - 03:18 PM

Ok, now i'm getting the same error "operand types do not match" in the following lines of code in the Numbers from the User section:

```mov num1,b1

mov num2,bh

```

```.model small
.stack 100h
.data
prompt1 db 13, 10, 'First number','\$'
prompt2 db 13, 10, 'Second number','\$'
result db 13, 10, 'input number','\$'

;Variables

num1 dw ?
num2 dw ?
sum dw ?

.code
main proc

mov ax,@data		   ;get data segment address
mov ds,ax	;initialize ds

;Display prompt

mov ah,9	;print string function
mov dx,offset prompt1   ;ds:dx points to string
int 21h

; Numbers from the user
mov ah,1 	;input function
int 21h
mov bl,al 	;save the value from input
mov num1,bl

mov ah,9
lea dx, prompt2		 ;print prompt
int 21h

mov ah,2 	;input second function
int 21h
mov bh,al 	;save the value from second input
mov num2,bh

sub num1,'0'
sub num2,'0'
mov ax,num1			;move num1 into ax
mov sum,ax			 ;move the total sum of numbers in sum

;Print Sum
mov ah,9
lea dx, result		 ; print result
int 21h

mov ah,2
mov dl,bl
int 21h

mov dl,'+'			 ;display + sign
int 21h

mov dl,bh
int 21h

mov dl,'='			 ;display = sign
int 21h

mov dl,bh
int 21h

mov ah,4ch
int 21h

main endp

end main

```

### #6 qwert12345

Reputation: 0
• Posts: 169
• Joined: 26-October 08

## Re: Prompt the user to enter two numbers to add and display the sum

Posted 27 January 2010 - 05:16 PM

I'm still lost....I keep changing bl and bh registers around for num1 and num2 and i keep getting the same error "operand types do not match" . and this program is due tomorrow

This post has been edited by qwert12345: 27 January 2010 - 05:16 PM

### #7 Martyn.Rae

• The programming dinosaur

Reputation: 538
• Posts: 1,406
• Joined: 22-August 09

## Re: Prompt the user to enter two numbers to add and display the sum

Posted 28 January 2010 - 04:09 AM

ok, here is the code for reading the user input.

```;Display Prompt

mov ah,9					;print string function
mov dx,offset prompt  ;ds:dx points to string
int 21h

mov ah,1					;input function
int 21h
xor ah,ah					;clear the top 8 bits of ax
mov num1, ax			 ;save the value from input

mov ah,9
lea dx, prompt2		;print prompt
int 21h

mov ah,2					;input second function
int 21h
xor ah, ah				;clear the top 8 bits of ax
mov num2, ax			 ;save the value from second input

```

ah is the top 8-bits of ax and al is the bottom 8-bits.

This post has been edited by Martyn.Rae: 28 January 2010 - 04:10 AM