News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Processing Hexidecimals with Boolean Operators

Started by Syre Lancaster, September 16, 2013, 07:43:18 PM

Previous topic - Next topic

Syre Lancaster

Hello all,
First time poster here (and MASM noob) and I have a question regarding Hexadecimals. I've been working on this problem in my book for the chapter and it asks me to make a boolean calculator that will allow the user to enter two hexadecimals, process them with AND, OR, NOT or XOR and spit out the answer. So far I have a functional code that does all of that, but it fails to spit out the correct answer  :eusa_boohoo:

So, my question to all of you, is how am I not processing this bit of code correctly?

Process_A PROC
mov edx,OFFSET str1
call WriteString
call ReadHex
mov edx,OFFSET str2
call WriteString
call ReadHex

mov al,str1
AND al,str2

mov edx,OFFSET post
call WriteString
call WriteHex
call Crlf
call WaitMsg

exit


;code
Process_A ENDP


*Please note that str1,str2 and post are intialized as BYTE in the .data
*If needed, I can post my entire code, but I think the error lies within this module.
*Warn me if I am about to bring down the wrath of the mods via content of this post. :dazzled: I'm brand new here!

MichaelW

If you are using the irvine32 library, ReadHex returns a 32-bit integer in EAX, and WriteHex takes its input from EAX. So it looks like for each pair you should be doing something like:

    call ReadHex
    mov ebx, eax
    call ReadHex
    and eax, ebx
    call WriteHex


Note that EAX is in the destination operand position for the AND instruction, so the result will go into EAX.
Well Microsoft, here's another nice mess you've gotten us into.

Syre Lancaster

Made the changes you said and here is the result. Added more comments for shininess.
Tested it with:
1st:      00008Af2
2nd:     0000FF00
Result:  00008A00

I'm no expert with binary arithmetic, whats a good way to tell if the answer was correct? A online popular online calculator perhaps?

Process_A PROC
mov edx,OFFSET str1     ;Enter first hex prompt
call WriteString    ;Display message
call ReadHex        ;Read the input
mov ebx,eax ;move hex content to EBX
mov edx,OFFSET str2 ;Enter second hex prompt
call WriteString ;Display message
call ReadHex ;Read the input
AND eax,ebx ;AND cotents of EAX and EBX

mov edx,OFFSET post     ;Results out
call WriteString ;Display message
call WriteHex       ;Display hex results
call Crlf ;Spacer
call WaitMsg ;Wait for user to continue

exit
Process_A ENDP


*Does the code look good?

GoneFishing

Calc.exe in "Programmer mode" (menu View -> Programmer or Alt-3) is the best for this kind of operations  :t
And yes, your answer was correct