Spot the bug in x86 program! (very frusterated)

Started by
3 comments, last by qesbit 19 years, 2 months ago
Basically I'm trying to make a boolean calculator, which you can use AND, OR, NOT, and XOR with. The user makes thier choice of what to do, they input the appropriate number of arguments, and then the result is displayed. However, the result is rarely correct. I have on idea what is causing it, so I could really use your help :).
TITLE Simple Boolean Calculator                  (BoolCalc.asm)

; *** NOT WORKING PROPERLY ***

; Program Description: A simple boolean calculator with a menu.
; Author: BBG
; Creation Date: Feb 13 2005
; Revisions:
; Date:             Modified By:

INCLUDE Irvine32.inc

.data
	menu BYTE	"1. x AND y",0Dh,0Ah,	; The menu to display
			"2. x OR y",0Dh,0Ah,
			"3. NOT x",0Dh,0Ah,
			"4. x XOR y",0Dh,0Ah,
			"5. Exit program",0
	choice BYTE "What is your choice? ",0
	prompt1 BYTE "Enter the first value:  ",0
	prompt2 BYTE "Enter the second value: ",0
	result BYTE  "The result is: ",0

	value1 DWORD ?
	value2 DWORD ?

.code

main PROC
	mov edx, OFFSET menu
	call WriteString		; Just writes the value of menu to the screen
	call Crlf			; Next line
	call Crlf
	mov edx, OFFSET choice
	call WriteString		; Prompt user for their choice

	call ReadInt	; Read their choice
	mov ebx,eax	; Their choice is stored in eax with ReadInt, so move to ebx

	cmp ebx,5
	je quit			; If their choice is 5 just quit

	mov edx,OFFSET prompt1
	call WriteString		; Prompt for first value
	call ReadHex			; Read in the hex value
	mov value1,eax

	cmp ebx,3		; If choice is 3 (NOT), skip second prompt
	je L3

	mov edx,OFFSET prompt2	; Prompt for second hex value
	call WriteString
	call ReadHex
	mov value2,eax

start:				; Find out where to go
	cmp ebx,1
	je L1
	cmp ebx,2
	je L2
	cmp ebx,4
	je L4

L1:	mov 	eax,value1	; logical AND
	and 	eax,value2
	jmp 	display
L2:	mov 	eax,value1	; logical OR
	or 	eax,value2
	jmp 	display
L3:	mov     eax,value1	; logical NOT
	not 	eax
	jmp 	display
L4:     mov     eax,value1		; logical XOR
	xor 	eax,value2
	jmp 	display

display:
	mov edx,OFFSET result	; Write result string
	call WriteString
	call WriteHex		; Value to write stored in eax, output it
	call Crlf	        ; next line

quit:
    exit
main ENDP

END main
Below are some sample outputs:
Quote:1. x AND y 2. x OR y 3. NOT x 4. x XOR y 5. Exit program What is your choice? 1 Enter the first value: f0f0f0f0 Enter the second value: ffffffff The result is: F0F0F000 Press any key to continue . . .
Quote:1. x AND y 2. x OR y 3. NOT x 4. x XOR y 5. Exit program What is your choice? 2 Enter the first value: f0f0f0f0 Enter the second value: ff00ff00 The result is: F0FFF000 Press any key to continue . . .
Quote:1. x AND y 2. x OR y 3. NOT x 4. x XOR y 5. Exit program What is your choice? 3 Enter the first value: f0f0f0f0 The result is: 0F0F0FFF Press any key to continue . . .
Quote:1. x AND y 2. x OR y 3. NOT x 4. x XOR y 5. Exit program What is your choice? 4 Enter the first value: f0f0f0f0 Enter the second value: f00f00f0 The result is: FFF00000 Press any key to continue . . .
It almost seems as if the result has been shifted left one byte... but why?!?! Please help :-( [Edited by - Nietsnie on February 13, 2005 1:09:16 AM]
Advertisement
It's not shifting it left by three bits, but by one byte. My assembler is a little rusty, but I can't see how the result is getting to WriteHex. You seem to do the operation in eax, and them immediately replace it with value1 after 'display'. The call to WriteString also has to potential to currupt eax before WriteHex can get at it.
Quote:It's not shifting it left by three bits, but by one byte.
Oops.. it's late :)

Quote:but I can't see how the result is getting to WriteHex. You seem to do the operation in eax, and them immediately replace it with value1 after 'display'. The call to WriteString also has to potential to currupt eax before WriteHex can get at it.
And yet another typo :)... I had the line "mov eax,value1" in there to test it and forgot to take it out. (On a side note, it doesnt even return value1 properly). WriteHex works by taking the value stored in eax and writing it to the console. Also, WriteHex does nothing to eax in any way (I even tried pushing/popping eax around the writestring and it still came out the same).

Thanks for the help!! Keep it coming :)
Try a "move eax, BAADF00Dh" or something before WriteString (and then WriteHex) at the bottom. If that stuffs up, you know it's in either of those two calls. If it doesn't stuff up (and value1 doesn't get printed correctly, like you had before), you'll know the problem is probably in the ReadHex.
Does WriteCall preserve eax? Does it use eax?

{ED]
Probably should verify ReadHex works first :-)

This topic is closed to new replies.

Advertisement