News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Adding in asm

Started by Unkownname, May 31, 2007, 07:56:28 PM

Previous topic - Next topic

Unkownname

Hello everyone

Ive been working out this code to add two numbers but i cant seem to get it working it outputs letters Im probably not understanding the "add" ability all to well. Could someone tell me what IM doing wrong :)

.model small
.stack

.data
nm_1 db "First Num",10,13,"$"
nm_2 db "Second Num",10,13,"$"
nm_3 db "They Equal:$"
nl db " ",10,13,"$"
n_1 db 0
n_2 db 0

.code
main:
loop1:
;Print out First Num
mov   ax,seg nm_1
mov   ds,ax
mov ah, 09
lea dx, nm_1
int 21h

;Get Second Number and Store
mov ah,08
int 21h
mov n_1,al
mov dl,n_1

;Output Number
mov ah,02
int 21h
;Insert a new Line
mov   ax,seg nl
mov   ds,ax
mov ah, 09
lea dx, nl
int 21h

;Get Number 2

;Print out Second Num
mov   ax,seg nm_2
mov   ds,ax
mov ah, 09
lea dx, nm_2
int 21h

;Get Second Number and Store
mov ah,08
int 21h
mov n_2,al
mov dl,n_2

;Output Number
mov ah,02
int 21h

;Insert a new Line
mov   ax,seg nl
mov   ds,ax
mov ah, 09
lea dx, nl
int 21h

;Add them up and Output :)
mov ax, seg nm_3
mov ah,09
lea dx, nm_3
int 21h

mov al,n_1
mov bl,n_2
add al,bl
mov dl,al
mov ah,02
int 21h
;Insert a new Line
mov   ax,seg nl
mov   ds,ax
mov ah, 09
lea dx, nl
int 21h

;Restart Loop
jmp loop1

;Quit
MOV AH,4Ch
MOV AL,00
INT 21h;
end


2nd day doing asm and yes I wrote all that myself

MichaelW

Functions 08 and 02 deal with characters, meaning that function 08 returns a character code in AL and function 02 expects a character code in DL. Instead of adding the numbers that the characters represent, you are adding the character codes, and then displaying the result as a character. For example, suppose both numbers are input as "1". The ASCII character code for "1" is 49, and 49+49=98, which is the ASCII character code for "b". To do what I think you are trying to do you must convert each input character to the number that the character represents, add the numbers, and then convert the resulting number to the character code of the character that represents the number. This is fairly straightforward if only single decimal digit numbers are involved:

number = character code(character) - character code("0")
character code = number + character code("0")

For example, the character code for "1" is 49 and the character code for "0" is 48, so assuming the input character is "1":

49 - 48 = 1

and

1 + 48 = 49

For decimal numbers with more than one digit the conversions are more complex, but still not difficult.
eschew obfuscation

Unkownname

so is my whole code a loss?

MichaelW

Your code is good for a second day effort, it just needs some changes. You have not stated what your exact goal is, so I have no way of knowing what those changes should be. BTW, you should be getting this warning when you link:

LINK : warning L4038: program has no starting address

Indicating that you should specify a starting address. The code as it is laid out works OK without specifying a starting address, but with other layouts it might not. The normal way of doing this would be to specify the intended program entry point address after the END directive, as in "end main".
eschew obfuscation