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

There was actually one thing I didn't manage to do.
And that was check for certain overflow errors.

Because I am using 32 bit signed integers, that means maximum 10 digits per int before the basic equation is overflowed.
However that also means that the sum, difference, product, quotient and remainder can't also exceed 10 digits.
I didn't know how to check for that though because adding two 10 digit numbers together will produce something like -1023111031 which
is also 10 digits. It is clearly overflow, but the program doesn't catch it automatically and I don't know how to write code to catch it either.

However, everything else seems perfect, so I will upload the final :D


EDIT: I used Kip Irvine's library
To see his functions that I used, download the file IrvineLibHelp from here:
http://kipirvine.com/asm/files/index.html
He provides an explanation as well as the actual code for each function.


RuiLoureiro

Hi,
       Your prog is hard to follow it is not easy for me...
        upload your .exe file

Quote
didn't know how to check for that though because adding two 10 digit numbers
together will produce something like -1023111031 which ...
         When you convert a 10 digit number you can check for overflow
         When you add 2 numbers you can check for overflow ... etc

         add    eax, ebx
         jc     _overflow           ; goes here and exit                             

RuiLoureiro

Quote
n1:             ;Starts putting the digits together into one number in Eax
add overflowcounter,1
sub al,30h
mov ebx,0
mov bl,al
mov eax,num1
imul eax,10
add eax,ebx
mov num1, eax

            we dont need to count digits ( overflowcounter )
           
sub     al, 30h
movzx   ebx, al
mov     eax, 10
mul     num1
jc      overflow            ; define where to put this label
add     eax,ebx
jc      overflow            ; define where to put this label

mov     num1, eax           ; this number has not signal

            at the end we need to test the sign bit (bit 31):
            if set than overflow (we are working with 31 bits)
            (after/before    mov   operator, al)
            Or use  js   instead

krikara

Hey so that calculator was once a finished a project, but now I have a new assignment to make it even better :S.

Part of the assignment is to have the calculator have heximal and binary signed integers as input too and outputing the number in the format the first number was entered in.

So if a heximal + binary = output in hexidecimal.

I don't think this should be too hard , just some extra lines of code, but there was something I was wondering about the ADD SUB IMUL and IDIV instructions.

I already know they are capable of doing decimal integer arithmetic, but my text book never clearly stated whether they are capable of doing math in binary and hexidecimal. If they are capable of doing so, that would make my life a tad bit easier. If not, I think I would have to convert everything to decimal and do the arithmetic, and then reconvert the numbers to whichever the first number format was inputted in.

Decimals end in integers like 1000. Hexidecimals end with an h like 09Fh. and Binary numbers end in b like 1010101b.

_______________________________________

Edit:
My post may be unclear, so let me restate my questions.

Can the ADD SUb, etc instructions perform arithmetic in hexidecimal and binary
such as Hexi + Hexi = Hexi and Binary + Binary = Binary

and if so
can I do a cross between them? For example , can I do Hexi + Binary? If that works, then what format will be outputted?

dedndave

binary is essentially the native tongue of the processor
hexidecimal is merely a human-friendly way to express binary values

in truth, you may consider 32-bit unsigned math as base 4294967296   :P
(a 32-bit register might be thought of as a single "digit", with 4294967296 possible values)
but - it is a binary-based representation

krikara

Interesting enough, when I tried adding 011b (binary number) + 001h( hexidecimal) and called WriteInt (which outputs in decimal),
the answer is correct.

This is mind blowing, but now I am starting to understand what you meant earlier by like 4294967296    bases.

btw I use WriteInt (outputs decimal value for eax), WriteBin(outputs binary value), and WriteHex (outputs hex value) from KipIrvine's library.

krikara

Okay this poses a new problem with my program.

Before, the way my program worked was I inputted a string such as 44+55=

and it would output +99

my .data had where the program would evalute the numbers and load them into num1 and num2 accordingly
num1 DWORD 0
num2 DWORD 0
operator BYTE ?

For example the original program scans 4 and puts it as num1. It scans the next 4, and adds it to num1*10. Then it scans operator.
but now the input also has to allow input such as 0A4h+010b=
and thus, I have to rework my whole algorithm.

I am trying to find the shortest way to do so, but I can only think up of one solution.
That is to store num1 and num2 in strings instead. And then store num1 and num2 strings into DWORDs, where it checks the last letter to see if it is decimal, binary, or hexa. I was just wondering if there is a faster solution/algorithm I can use that require less code that will complete this arithmetic?

dedndave

i would probably try to write it as a parser loop
but, that may not be the best way to do it   :P