Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • 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!

Join Tek-Tips
*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.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

tntcoder (Programmer) (OP)
25 Dec 05 18:17

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.
Helpful Member!  earthandfire (Programmer)
25 Dec 05 18:38
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.

tntcoder (Programmer) (OP)
25 Dec 05 21:08
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.

cpjust (Programmer)
26 Dec 05 4:05
Does int 21h work on Windows?

What if you call printf?  That might work better.
earthandfire (Programmer)
26 Dec 05 6:47
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.

tntcoder (Programmer) (OP)
26 Dec 05 9:02
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.
cpjust (Programmer)
26 Dec 05 15:30
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
zeitghost (Programmer)
26 Dec 05 15:51
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...

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!

Back To Forum

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