News:

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

Help in addition

Started by tkhan, April 13, 2009, 03:49:12 PM

Previous topic - Next topic

tkhan

Hello.

I am fairly new to 16-bit programming using MASM. I have made a procedure to add two numbers. However it is returning me some weird character as output. Please help me in this regard

Thanks.

here is my code

.model large,stdcall
.stack 1024h
.686

AddNumbers PROTO, val1:byte, val2:byte

.data

.code

main proc


INVOKE AddNumbers, 1,4


mov ax,4c00h
int 21h

main endp

AddNumbers proc, val1:byte, val2:byte
mov bl,val1
mov bh,val2

add bl,bh
sub bl,30h

mov dl,bl
mov ah,02h
int 21h

ret

AddNumbers endp

end main


MichaelW

To convert a numeric value in the range 0 to 9 to a decimal digit you must add 30h (the ASCII character code for the decimal digit '0') to the value.

Also, your code is not initializing DS to the program's data segment. Although it does not matter for your current code, it will matter for any .EXE program that accesses the data segment. Two possible methods are to use the .startup directive, like this:

.code
. . .
.startup
. . .

Or use the predefined symbol @data, like this:

mov ax, @data
mov ds, ax
eschew obfuscation

tkhan

Quote from: MichaelW on April 13, 2009, 04:28:49 PM
To convert a numeric value in the range 0 to 9 to a decimal digit you must add 30h (the ASCII character code for the decimal digit '0') to the value.

Also, your code is not initializing DS to the program's data segment. Although it does not matter for your current code, it will matter for any .EXE program that accesses the data segment. Two possible methods are to use the .startup directive, like this:

.code
. . .
.startup
. . .

Or use the predefined symbol @data, like this:

mov ax, @data
mov ds, ax


Thanks alot MichaelW. It worked as you said :D

However I now have one more problem: adding 30h doesnt help me in multiplication. Code is:

.model large
.stack 1024h

.data
message db "Enter a number to calculate its multiple:$"

.code

main proc

mov ax,@data
mov ds,ax

mov dx,offset message
mov ah,09h
int 21h

;new line
mov ah,02h
mov dl,0dh
int 21h
mov dl,0ah
int 21h

;input
mov ah,01h
int 21h


mov bl,al
mul bl   

add al,30h

mov dl,al


mov ah,02h
int 21h

mov ax,4c00h
int 21h

main endp

end main

Your help will be appreciated :)


FORTRANS

Hi,

   Presumably your input is an ASCII digit.  You need to subtract the
ASCII zero to convert it into a binary number.  And unless you modify
your code to output multiple digits, limit the input to 0 through 3.

Regards,

Steve N.

tkhan

Quote from: FORTRANS on April 13, 2009, 05:11:52 PM
Hi,

   Presumably your input is an ASCII digit.  You need to subtract the
ASCII zero to convert it into a binary number.  And unless you modify
your code to output multiple digits, limit the input to 0 through 3.

Regards,

Steve N.

You mean instead of code in line: add al,30h
i should sub al,30       ????

i tried it but it still gives me a strange character output. Can you plz tell me that exact code to correct my problem? thanks.

dedndave

well - you need to:
1) subtract 30h from each input digit to create the binary
2) mul and add the digits together (100Xhundreds + 10Xtens + ones, etc)
3) convert the resulting binary back to ascii

there are a few special instructions just for this stuff

AAM, AAD, DAA, to name a few
I rarely use these instructions, so I don't remember them all
But, if you google those, you will find the others, as well as some examples of their use

tkhan

Quote from: dedndave on April 13, 2009, 07:14:41 PM
well - you need to:
1) subtract 30h from each input digit to create the binary
2) mul and add the digits together (100Xhundreds + 10Xtens + ones, etc)
3) convert the resulting binary back to ascii

there are a few special instructions just for this stuff

AAM, AAD, DAA, to name a few
I rarely use these instructions, so I don't remember them all
But, if you google those, you will find the others, as well as some examples of their use


Thanks. It worked :D

thanks to everyone!