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!

help, on addition and substraction with user inputs

Status
Not open for further replies.

msab

Vendor
Apr 10, 2002
1
0
0
US
Im trying to create calculator...but rt now just trying the addition and subtraction numbers with user input. Im using 16 bits DOS o/s. I can sure used some help. This is part of my codes...

my add and sub code is not function correctly.


input:

mov dx, userinput
call Writestring
call Readint
call Crlf
mov bx,10
call Crlf
mov al, userinput

;user input operation proc(-,+,/,*):

mov dx, offset operator
call Writestring
call Readstring

ja opercmp ; jump to operation procedure


; operation proc

opercmp:
cmp al,43 ; value for + sign
je add1 ; add1 label proc

cmp al,45
je sub1 ; sub1 label


;storage1 the value

stor1:

cmp storage1,0
jne stor2
mov stor1,al
jum input

;storage2 value

mov storage2, al
jmp input


addition proc:

add1:

push bx
push cx
mov ax,0
cs1: add ax,[bx]
add bx, 2

loop cs1
pop cx
pop bx

ret
addition endp


subtraction proc:

sub1:

push bx
push cx
mov ax,0
cs2: sub ax,[bx]
sub bx, 2

loop cs2
pop cx
pop bx

ret
subtracition endp


 
Hi,

Its a bit difficult to see how you are storing your input numbers.

Is the input string stored in memory packed to the right? this means that the fixed location of the string will always be the lowest denominator (x1 and not x10).

This will make it easier to convert the input string into binary. You have to decide if the binary number will be a signed or unsigned integer or floating point number.

I suggest you convert the number into a signed integer as this will enable you to take advantage of the processor calculating the results correctly first hand. a signed integer has its highest bit on for a minus number.

If you store the string normally you can write a routine to find the end and then read it backwards. each digit you read is then multiplied by its denominator and added to the total. if at the end you find a minus sign i suggest you subtract the total from zero to give you a twos comliment negative number.

Thats how to convert a decimal number but you can convert any type of number into binary. once in binary the calculations are easy.

are you converting your numbers into binary correctly?

in your routines-
you are adding every consecutive word from starting from[BX], the number of consecutive words to add in CX.

word=2 bytes
[ ] = location in memory, [BX]=location pointed by BX

have you set BX to point to your stored variables corrctly?

I cant see where you set CX, how many consecutive words do you want to add?

i notice when you 'ADD' the memory pointer BX moves up memory and when you 'SUBTRACT' it moves down.

you can only use RET command if the procedure is called! i would try somting like this:

you had:
cmp al,43 ; value for + sign
je add1 ; add1 label proc
cmp al,45
je sub1

i suggest:
cmp al,43
jne notadd
call add1
jmp notsub
notadd: cmp al,45
jne notsub
call sub1
notsub:

if all your procedures preserve registers then you can remove 'jmp notsub' doing this will save memory but not speed.

remember when you use mul and div the DX register is also used. especially using div, DX must be the high word of the value to be divided, DX must be set to zero if the number fits into AX.

you can use EAX, EBX ect in 16bit real mode. this will allow you to have larger signed integers.

I would only worry about floating point binary numbers if you are interested in calculus or trigonometry etc.

Straiph
0000:0000:0000:0000h
The people who have nothing to say and say it too loud have little knowledge, It's the quiet ones you need to worry about!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top