News:

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

Simple Arithmetic Input 8086

Started by krikara, November 22, 2010, 09:48:38 AM

Previous topic - Next topic

krikara

Okay so I am working on a program that needs to be able to to read input such as
"5 + 100"
And then out put 105.
The program is supposed to be able to perform any basic arithmetic including mod.

Reading integers are pretty easy because all I had to do was
call ReadInt
mov num1,eax

However I am confused what to call to read the sign.
Basically, I want to know how to read the input for the operator.
Right now, I have the following

call ReadInt
mov num1,eax
call ReadChar
mov operator,al
call ReadInt
mov num2,eax


However, this does not read my input correctly. I used DWORD for num1 and num2 and BYTE for operator. It seems to only register
the ReadInt commands because it only allows me to push enter twice before ending the program and if I enter 5 then enter 6, EAX comes as 6 in dumpregs.

__________________________________________________________________
I have a basic idea on what do to with the operator after the program reads it(although I am not sure if this idea works).
I would do something like
cmp op, '+'
jmp addtion

cmp op, '-'
jmp subtraction

and so on.
Help soon would be much appreciated.
Thanks

krikara

So apparently the line
call ReadChar
was working, but I didn't have to push enter after that because it only reads one char.

I finally discovered it worked after calling WriteChar :S
So now I just have to figure out how to use that input and make it do the appropriate arithmetic.
I will try attempting my method.

___________________

edit: it seems my method does not work where I do
cmp op,'+'
jmp addition

cmp op,'-'
jmp subtraction ......

How do I fix the code such that it compares the operator to the + - * / % signs?

jj2007

Try to read a full line with Masm32 lib StdIn:
include \masm32\include\masm32rt.inc

.data?
buffer db 1000 dup(?)

.code
AppName db "Masm32:", 0

start: print "Type something, then press Enter", 13, 10
mov esi, offset buffer
invoke StdIn, esi, sizeof buffer
.Repeat
lodsb
movzx eax, al
.break .if !eax
print str$(eax), " "
.Until 0
print esi, 13, 10, "That was your input", 13, 10
exit

end start

krikara

So far this is my code.
ReadInt and ReadChar works appropriately. However, the line that doesnt function is cmp operator,'+'
How am I supposed to write the code such that it will successfully compare operator to each individual sign (+  -  *  /  %)


call ReadInt
mov num1,eax
call ReadChar
mov operator,al
call ReadInt
mov num2,eax

mov al,operator ;I added this just incase
cmp operator,'+'
jmp addition

addition:
mov eax,num1
add eax,num2
call WriteInt


The addition surely works fine, however if I input 5 - 6, the program still does the addition. And that should not happen. How am I supposed to compare the Char input to a sign?

I even tested
mov al,operator ;I added this just incase
cmp al,'+'
jmp addition


And the program still jumps to addition even when i input 5-6

dedndave

well - we cannot see the entire construct of the program, as posted
is this a 16-bit or 32-bit program ?
are you using Kip's library ?
are you using MASM ?

krikara

Ahh yes 32 bit
Using Kips Library and MASM

INCLUDE Irvine32.inc

.data
num1 DWORD ?
num2 DWORD ?
operator BYTE ?

.code

And so on

hutch--

krikara,


"5 + 100"


With a task of this type you are writing a simple parser and you need to be able to split it up into its components.


1. 5
2. +
3. 100


I am not familiar with Kip Irvine's libraries but at a coding level the way to do this is to write a 256 character table which you can fill with zeros, the number positions 1s and the arithmetic operators 2 just as an example.

Then you scan the string from beginning to end recognising only the numbers and operators.

Write each item to a memory buffer then perform the arithmetic operation based on the operator, "+" in this instance.

Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

dedndave

here in the forum, many of us are not all that familiar with Kip's libraries
the masm32 package that most of us use contains a set of libraries and macros
as Hutch mentioned in this thread, mixing libraries may no work so well
at any rate, we could use a little more info on the functions you are using   :P

krikara

Okay.

So far, I only called 2 different functions
ReadInt and ReadChar

ReadInt basically reads a 32 bit signed integer from user input in command prompt and stores it into eax. So I basically push
5, then enter and it stores 5 into eax.
Then I do mov num1,eax so it stores my inputted number into num1

ReadChar basically reads any single character. So once you type a character, it will be stored into al
And then I do mov operator,al so operator gets the operator sign stored, which should be one of these
(+  -  *  /  %)

I'm not sure I understand to the full extent of what Hutch said , but I have the operator stored and can move it into the AL register anytime I want. The problem I have is figuring out how to match the input operator to the correct function.
So if I input + , it should go to the addition function, if I input *, it should go to the multiplication function.
I thought cmp operator,'+' would work, however, that is not producing correct results because the program keeps going to the
addition function regardless of any operator I input.

dedndave

cmp operator,'+'
jmp addition


jumps to "addition" no matter what the results are of the CMP instruction
the JMP instruction is unconditional - that means it branches to the label, regardless of the flags
JZ branches if the zero flag is true (cmp sets the flags the same as sub)

it might look something like this:

        mov     al,operator
        cmp     al,'+'
        jz      addition

        cmp     al,'-'
        jz      subtraction

        cmp     al,'*'
        jz      multiplication

        cmp     al,'x'
        jz      multiplication

        cmp     al,'X'
        jz      multiplication

        cmp     al,'/'
        jz      division

        print   chr$('Invalid Operator'),13,10
        jmp     try_again


notice that if it fails all the tests for valid operator characters, it "defaults" to an "invalid operator" message and allows the user to try inputting again

also notice that PRINT and CHR$ are not part of Kip's lib
you may want to find his eqivalent functions and replace them

also...
notice that i moved the value of the operator into AL
then CMP AL,xx
this generates shorter and faster code
not that speed is an issue in this case, it is just good practice

krikara

replacing jmp with jz seems to work so far with addition and subtraction. Let me finish the rest of the program and test  :cheekygreen:

Although I don't quite understand why it didn't work with jmp.

doesn't cmp check if operator = '+'
and if it is true, it jumps to addition?

Or does it just auto jmp to addition because it jumps regardless whether operator is + or not.

dedndave

sorry - i updated my previous post and it should now answer your question   :P


krikara

Okay, that aside, I wanted to know if there is a possible way to input
"5 + 6" (1 input) and have the program sort out the numbers and operator respectively.

Currently, my program is written with
call ReadInt
call ReadChar
call ReadInt

And thus my input is "5" enter, "+" , "6" enter. I basically have 3 different inputs right now.

So my question is how do I input the string and have the all the elements sorted out appropriately.
If I entered "[x operator y]"
x should be stored into num1
operator should be stored into operator
y should be stored into num2

dedndave

you could input the entire line as text
then, search for the operator, using it as both an operator and a parameter seperator
so, the user enters something like "1234+5678"
you find the "+" - the stuff before it is one parameter - the stuff after it is another - ignore any spaces

i am not sure what the fastest way to find the seperator is, actually
and, in this case, it doesn't need to be all that fast
in the reference link i mentioned above, find the section on string instructions
you might use REP SCASB, in this case

another method might be a not-so-simple parser loop that loads characters and branches accordingly
this is probably the method i would use