Solved

TROUBLE ADDING TWO NUMBERS IN ASSEMBLY

Posted on 2007-11-26
8
4,957 Views
Last Modified: 2008-02-01
I believe the disp_sum (last) call is incorrect because the program will execute but the sum will not be displayed.
Project is due tomorrow (11/27) so prompt assistance will be greatly appreciated
Thanks in advance!

; A Simple Calculator that performs basic arithmetic (+ - * /)

.model small

.stack 256

CR equ 13d
LF equ 10d

.data

promptNum1 db 'Please Enter The First Number:', 0 ;string terminated by 0
promptNum2 db CR, LF,'Please Enter The Second Number:',0 ;string terminated by 0
sum db CR, LF, 'The sum Of The Two Numbers Is:', 0 ;string terminated by 0
num1 dw ?
num2 dw ?
.code

start:

mov ax, @data ;set ds register to point to data segment
mov ds, ax ;since you cannot assign a segment to ds directy
mov ax, offset promptNum1 ;store memory address of promptNum1 in ax

call display_str ;calls the display_str to display promptNum1 string
call read_input ;read first number entered by user

mov num1, ax ;store the number entered by user into num1 variable
mov ax, offset promptNum2 ;store memory address of promptNum2 into ax

call display_str ;calls the display_str to display promptNum2 string
call read_input ;read second number entered by user

mov num2, ax ;store the second number entered by user into num2 variable
mov ax, offset sum ;store memory address of sum into ax

call display_str ;calls the display_str to display sum string

mov ax, num1 ; ax = num1 ;move contents of num1 variable to the ax register
add ax, num2 ; ax = ax + num2 ;add contents of num2 variable to the existing contents of ax

call putn ; display sum ;call putn function to display the value of sum
mov ax, 4c00h
int 21h ; finished, back to dos

display_str: ;definition of display_str which displays string terminated by 0
push ax ;save contents of register ax
push bx ;save contents of register bx
push cx ;save contents of register cx
push dx ;save contents of register dx
mov bx, ax ;store address in bx (required for display)
mov al, byte ptr [bx] ;copy address of first character in string to al register

display_loop: cmp al, 0 ;while loop, while al != 0
je display_rest ;if last result was 0, then go to display_restore
call disp_char ;display character, one at a time, while al != 0
inc bx ;loop control, bx = bx + 1
mov al, byte ptr [bx] ;display next character in string
jmp display_loop ;go back to beginning of loop

display_rest:
pop dx ;restore register to beginning value
pop cx ;restore register to beginning value
pop bx ;restore register to beginning value
pop ax ;restore register to beginning value
ret


disp_char: ;display character stored in al, called by display_loop
push ax ;save contents of register ax
push bx ;save contents of register bx
push cx ;save contents of register cx
push dx ;save contents of register dx
mov dl, al ;move contents of al into bl (required for display)
mov ah, 2h ;call DOS display subprogram
int 21h ;interrupt to call subprogram
pop dx ;restore register to beginning value
pop cx ;restore register to beginning value
pop bx ;restore register to beginning value
pop ax ;restore register to beginning value
ret

read_input: ;read character into al
mov ah, 1h ;call DOS input subprogram
int 21h ;interrupt to call subprogram
ret


disp_sum ; display string terminated by 0
; dx contains address of string
mov dh, ah
mov ah, 2h
int 21h
ret


end start
0
Comment
Question by:khwajakhurram
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 5

Expert Comment

by:abith
ID: 20355974
>> disp_sum ; display string terminated by 0
  is disp_sum label ?
if so ':' is missing
0
 

Author Comment

by:khwajakhurram
ID: 20357418
no, that didn't do the trick. I think I may be using the wrong registers.
The sum is stored in the ax register and I want to display it on the screen
Thanks
0
 

Author Comment

by:khwajakhurram
ID: 20357452
one more error, the putn statement after the addition should say disp_sum.
This too does not fix the issue
Thanks
0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 
LVL 5

Expert Comment

by:abith
ID: 20357872
add ax, num2 ; ax = ax + num2 ;
    stores value in ax

disp_sum ; display string terminated by 0
; dx contains address of string
mov dh, ah
mov ah, 2h
int 21h
ret

int 21h      
    displays value originally from dl. but data moved to dh. that too only ah. (al value is left alone)

add value with al instead of ax.

if still you dont get the result, can you post and i/p you are using and o/p you are getting
0
 

Author Comment

by:khwajakhurram
ID: 20358782
Do you mean like this?
mov ax, num1
add al, num2

It doesn't compile. Invalid instruction operands
0
 

Author Comment

by:khwajakhurram
ID: 20358820
Here is the I/O when I run the program as is
Please Enter The Larger Number First :2
Please Enter Arithmetic Operation:+
Please Enter The Second Number:2
The Sum Of The Two Numbers Is:â– 
0
 
LVL 5

Accepted Solution

by:
abith earned 500 total points
ID: 20363519
like this

num1 db ? ; use db instead of dw
num2 db ?

mov al, num1
add al, num2

>> The Sum Of The Two Numbers Is:â–
i am not able to figure out the 2nd byte value. can you give me the ascii value of two result bytes
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VB.net Code Gerators 3 509
Help! Bomb phase 6 3 991
How to Replace 2003 failover Cluster node 4 367
wrong referenced assembly version  (using VS 2008) 3 242
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question