Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Adding Numbers

Adding Numbers

Adding Numbers


Please could someone tell me why the below code wont work, im trying to add two numbers together and display the result (both numbers are entered by the user). Im new to this, but as far as i can see ive done it correctly but it just outputs a random character that means nothing to me. Also this code will only work with single digit numbers, how would i go about allowing the user to enter a string of numbers, or anything longer than 1 digit?


.model small
.stack 100h

add_msga    db    10,13, "Enter Number 1: $"
add_msgb    db    10,13, "Enter Number 2: $"
add_num1    db  ?
add_num2    db    ?
add_result    db    ?

main PROC
mov ax,@data
mov ds,ax

call AddNumbers

mov ax, 4c00h
int 21h
main ENDP

AddNumbers PROC
;get num1
    mov ah, 09h
    mov dx, offset add_msga
    int 21h
    mov ah, 1h
    int 21h
    mov add_num1, al
;get num2
    mov ah, 09h
    mov dx, offset add_msgb
    int 21h
    mov ah, 1h
    int 21h
    mov add_num2, al

;add [numbers]
    mov al, add_num1
    add al, add_num2
    mov add_res, al
;output result
    mov ah, 09h
    mov dx, offset add_res
    int 21h


AddNumbers endp

END main

Thanks alot.

RE: Adding Numbers

The program is NOT doing what you think it is.

Assume the user input is:

You want the output to be:

Unfortunately your program is receiving:

and is outputting:
65h (a capital E)

This problem has been asked about and answered several times before - a search of this forum should prove very useful.  A recent thread that might help point you in the right direction (although more advanced than the problem in question is thread272-1150928)

Hope this helps.

RE: Adding Numbers

Thanks for that mate ive managed to get it working. I have another question though. If im to make this 32bit, how do you display a string correctly?

mov ah, 09h
mov dx, OFFSET t_msga
int 21h

That does not work when i put the program through a 32 bit assembler, how can i fix this? if i use edx instead it assembles, but the program crashes when it tries to load the memory address.


RE: Adding Numbers

Does int 21h work on Windows?

What if you call printf?  That might work better.

RE: Adding Numbers

cpjust what does printf have to do with assembler?

Surely that is a c/c++ or similar function - although it may be available in some high level assemblers, I can't see how that can help with straightforward assembly language.

RE: Adding Numbers

I have been running the 16bit version with INTs inside windows in a dos prompt without problems. So if i make it 32bit i take it i have to call 32bit functions and cant use INTs? is this correct. So would i have to import functions from C++ libarys or something.

RE: Adding Numbers

To call printf you just need to link with clib.lib.

I don't have a real assembler for Windows, so I can't try it out, but when I run the following code in Visual C++ it blows up.


    mov        dx,        'XXX'
    mov        ah,        9
    int        21h

RE: Adding Numbers

Just a thought: wouldn't it be better to get to know the nice simple 16 bit dos box type assembler before launching into the realms of 32 bit assembler?

Adding two numbers together & getting the correct answer seems a bit problematic at the moment...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close