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

The sum of the two numbers should be less than 10

Page 1 of 1

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

#1 qwert12345  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

;Addition
		mov ax,num1			;move num1 into ax
		add ax,num2			;add first and second numbers together
		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  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 qwert12345  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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".
Was This Post Helpful? 0
  • +
  • -

#4 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • 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
		add ax,num2		  ;add first and second numbers together
		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
		add al,num2		  ;add first and second numbers together
		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

Was This Post Helpful? 0
  • +
  • -

#5 qwert12345  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

;Addition
		sub num1,'0'
		sub num2,'0'
		mov ax,num1			;move num1 into ax
		add ax,num2			;add first and second numbers together
		mov sum,ax			 ;move the total sum of numbers in sum
		add sum,'0'


;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


Was This Post Helpful? 0
  • +
  • -

#6 qwert12345  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

#7 Martyn.Rae  Icon User is offline

  • The programming dinosaur
  • member icon

Reputation: 540
  • View blog
  • 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1