Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Adding Numbers 1

Status
Not open for further replies.

tntcoder

Programmer
Dec 10, 2005
6
GB
Hi,

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?

Code:
TITLE ADD
.model small
.stack 100h
.data

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	?

.code
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

	ret

AddNumbers endp

END main


Thanks alot.
 
The program is NOT doing what you think it is.

Assume the user input is:
2
3

You want the output to be:
5

Unfortunately your program is receiving:
32h
33h

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.



[vampire][bat]
 
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?

Using:
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.

Thanks
 
Does int 21h work on Windows?

What if you call printf? That might work better.
 
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.

[vampire][bat]
 
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.
 
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.
Code:
__asm
{
	mov		dx,		'XXX'
	mov		ah,		9
	int		21h
}
 
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...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top