Reputation: 155
; This program checks for even or odd parities inside of an array. Displays 1 if even or 0 if odd.
Include Irvine32.inc
TRUE = 1
FALSE = 0
.data
str1 BYTE "Even Parity", 0
str2 BYTE "Odd Parity", 0
str3 BYTE "The block of data under checking is: ", 0
array1 BYTE 1,0,1,0,0,0,1,0,0,1
array2 BYTE 1,0,0,0,0,0,1,0,0,1
; declares two 10-bit arrays
.code
main proc
call Clrscr ; Clears the screen
mov esi, OFFSET array1 ; Pass address
mov ecx, LENGTHOF array1 ; Pass length
call Display ; Display on Console
call Parity_Check
cmp eax,0
je L1 ; if EAX = 0 then odd
mov edx, OFFSET str1
call WriteString ; Write str1
call Crlf
L1:
mov edx, OFFSET str2
Call WriteString ; Write str2
call Crlf
call Crlf ; Check if array2 is even or odd
mov esi, OFFSET array2 ; Pass Address
mov ecx, LENGTHOF array2 ; Pass length
call Display ; Display on console
call Parity_Check
cmp eax, 0
je L2 ; if eax = 0 then odd
mov edx, OFFSET str1
call WriteString ;Write str1
call Crlf
L2:
mov edx, OFFSET str2
call WriteString ; Write str2
call Crlf
call Crlf
invoke ExitProcess,0
main endp
Parity_Check PROC USES eax ecx esi edi ;Returns 1 if even 0 if odd
mov edi, 0 ; array pointer 0
L1:
xor [esi], 0 ; Xor data bits
inc esi ; points to next bit
loop L1 ; continue loop
jpe L2 ; jumps to L2 if even
jpo L3 ; jumps to L3 if odd
L2:
mov eax, TRUE ; copies 1(true) to eax
jmp L4
L3:
mov eax, FALSE ; copies 0(false) to eax
L4:
ret
Parity_Check ENDP
Display PROC USES esi ecx edx ; Displays array elements
mov edx, OFFSET str3
call WriteString ; Writes str3
L1:
mov eax, [esi] ; Store array in eax
call WriteDec ; Write EAX
inc esi ; Point to next item in array
loop L1 ; Continue Traversing
call Crlf
ret
Display endp
end main
I can't figure out why I am getting an error for my XOR and a masm.target (file) error. The XOR says "Invalid instruction operand" while masm.targets error brings me to that file.
masm.targets is the file name, the error code is MSB3721 on line 50 column 5 (again it brings me to another page so I am assuming something is wrong with my MASM setup?). Any help with either of these?
Upvotes: 3
Views: 255
Reputation: 39166
array1 BYTE 1,0,1,0,0,0,1,0,0,1
array2 BYTE 1,0,0,0,0,0,1,0,0,1
; declares two 10-bit arrays
You actually declared 2 arrays of 10 bytes each.
xor [esi], 0 ; Xor data bits
MASM will complain for not knowing the size of this xor
operation. Just write it like xor byte ptr [esi], 0
jpe L2 ; jumps to L2 if even
jpo L3 ; jumps to L3 if odd
Both these parity related jumps will be based on the parity from incrementing the address in ESI. They don't reflect any parity value you got from testing the bytes in the array! Here's a routine that does:
Parity_Check PROC USES eax ecx esi ;Returns 1 if even, 0 if odd
mov al, 0
L1:
add al, [esi]
inc esi ; points to next byte
loop L1 ; continue loop
test al, 1
jnz L3 ; jumps to L3 if odd
L2:
mov eax, TRUE ; copies 1(true) to eax
jmp L4
L3:
mov eax, FALSE ; copies 0(false) to eax
L4:
ret
Parity_Check ENDP
In your Display procedure you forgot what is the true size of the array elements.
Change this
mov eax, [esi] ; Store array in eax
into
movzx eax, byte ptr [esi]
Upvotes: 4
Reputation: 8749
You're using a notation that treats [esi] as a reference, what you probably intended to do was xor esi, 0
-- but even so, such an operation isn't really meaningful (esi will remain unchanged.)
If you meant to modify the memory location identified by the value in 'esi' you may want to consider moving it into a register before the 'xor' operation, since I do not believe xor operates on memory operands (i could be mistaken, it has been a while for me.)
Upvotes: 3