News:

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

primitive type check in MASM?

Started by Ani_Skywalker, July 21, 2010, 05:56:44 PM

Previous topic - Next topic

Ani_Skywalker

Hi everybody,

I am a beginner that started MASM coding after I speed-read "The Art of Assembly Language" and "Introduction to 80x86 assembly language". Speed-read means I scooped interesting topics, read some 500 pages and so on. My problem is that I understand much better when I get to code then when I read page after page.

So, I am trying to build a basic calculator available to perform the 4 basic operands (+,-,*,/). So far, It's been going over exceptions, but not without some questions popping up, so here it goes:

; assume all the necessary includes are done and so on.

.data?
var1 dd, 0
var2 dd, 0

.code

start:
       
    mov var1, sval(input("Enter the first number: "))
    mov var2, sval(input("Enter the second number: "))
    print chr$(13,10)
    print str$(var1)
    print chr$(' ')
    print str$(var2)
    print chr$(13,10,13,10)

    xor eax, eax
   
    add eax, var1
    add eax, var2
   
    print str$(eax)
    print chr$(13,10,13,10)

mov eax, input("Press 'Enter' to exit. ")

exit



Questions:

1. Is there a way to _easy_ provide some kind of primitive-type check like those in high level languages to see if the user-input truly is an integer? If not, how do we do it the hard way then? We parse the input char by char and check if it is a number between 0-9?

2. I tried to add var1 and var2 to each other, like this: mov var1, var2. It did not compile and I guessed a register was the way to go, now I wonder, why is that so? Why is it not possible to add the two variables without adding them to a register?

3. Any other suggestions to my code?

As soon as I learn how to do type-control I move on to add the other operands and then a way to choose between the operands.

Thank you for your time!


ecube

yeah looping through each character in the string to make sure it's 0-9 is a good method.

Ani_Skywalker

Quote from: E^cube on July 21, 2010, 06:01:47 PM
yeah looping through each character in the string to make sure it's 0-9 is a good method.

Is there no other way?

If not, would it be a bad approach to learn how to create functions or classes in MASM, create a type-checking function/class in MASM and then re-use that function every time I need to type-check?

KeepingRealBusy

2. An assembler creates code to execute whatever the hardware has implemented, and the hardware has no memory to memory operations.

Dave.

Ani_Skywalker

Quote from: KeepingRealBusy on July 21, 2010, 06:08:22 PM
2. An assembler creates code to execute whatever the hardware has implemented, and the hardware has no memory to memory operations.

Dave.

Thank you! That answered it.

dedndave

well - there are MOVS and CMPS string operations   :P

as for the integer thing - i would probably parse until i see a decimal and make it be an integer

KeepingRealBusy

You are not allowed to say "Thank you." in this forum, nobody else does, why should you be allowed? :wink

KeepingRealBusy

Quote from: dedndave on July 21, 2010, 06:11:24 PM
well - there are MOVS and CMPS string operations   :P

as for the integer thing - i would probably parse until i see a decimal and make it be an integer

But these are hardware macros that use the esi edi registers to access memory.

Dave.

dedndave

i am not sure they are microcoded that way
they are native as far back as 8088/8086
at any rate, MOVS is ok - if you are moving a large amount of data
or a very small amount of data if it is "register convenient" and speed isn't critical
otherwise, you can probably write a faster loop   :P
CMPS isn't fast at all on newer CPU's - perhaps that indicates that it is microcoded in RISC

Ani_Skywalker

Quote from: KeepingRealBusy on July 21, 2010, 06:14:14 PM
You are not allowed to say "Thank you." in this forum, nobody else does, why should you be allowed? :wink

Sorry, but I saw a point in clearing out that I understood the explanation so nobody else tried to explain. It was not just a "Thank you" but also a notification that the questions answer had be found. But sorry, I wont repeat it.

About the MOVS and CMPS, I will google and read the tutorials on them. Always good to know.

KeepingRealBusy

Please, please, please observe the "Smiles" before you take someone literally. That was meant to be a funny poke at those that say nothing when you have given them a factual  response. I accep and appreciate your Thank you.

Dave.

jj2007

Quote from: Ani_Skywalker on July 21, 2010, 06:25:23 PM
About the MOVS and CMPS, I will google and read the tutorials on them. Always good to know.

Try \masm32\help\opcodes.chm - better than Google in this case.
There are also some practical hints here.

Welcome to the Forum :thumbu

clive

Quote from: Ani_Skywalker
Is there no other way?

If not, would it be a bad approach to learn how to create functions or classes in MASM, create a type-checking function/class in MASM and then re-use that function every time I need to type-check?

At the assembler (machine code) level you are working with the "bare metal" of the processor, and the processor is pretty stupid. The implementation of the processor has gotten a lot more complicated over the years, but if you look at the original CPU designs from the 1970-80's you will get an idea of the simple state machine, serial processing model.

You have to write subroutines to do even the most trivial task, or use subroutines written by someone else.

Most of the CompSci students coming here have to write routines to convert ASCII digits into binary numbers, convert binary into decimal, add things, compute powers, create RPN calculators, etc.

It could be a random act of randomness. Those happen a lot as well.

hutch--

The normal technique to filter characters is to perform the character input in an edit control and set up a subclass to filter characters. Processing the WM_CHAR message usually does the job fine.

If you are trying to pick it up from the console or command line you have no choice other than taking the string and checking it for non-allowed characters.
Download site for MASM32      New MASM Forum
https://masm32.com          https://masm32.com/board/index.php

Ani_Skywalker

Hi guys,

Thank you for all your suggestions. And trust me, I've been reading them all. I read the masm32-editors help-files and looked up the links that jj2007 provided. Also, I've been looking up the movs, movsb and cmps commands.

Still, I am unfortunate to tell you that I'm stuck right where I were yesterday. I can't seem to pick out a byte at a time to compare it's ascii value and see if it is in the range of the ascii code for the 0-9 numbers. There is where I'm stuck at the moment. And if you could get some help from there, I would really appreciate it.

I tried code like the code below, found in the MASM32 Editors help chms. Also tried several other approaches, always with compile errors:

cld               ; set direction flag forward
    mov esi, source   ; put address into the source index
    mov edi, dest     ; put address into the destination index
    mov ecx, ln       ; put the number of bytes to copy in ecx
  ; --------------------------------------------------
  ; repeat copying bytes from ESI to EDI until ecx = 0
  ; --------------------------------------------------
    rep movsb


If you look to the code above, the first problem with is that I don't get what source and destination should be, if this is to be adapted to my current code.