• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

x86 Assembly

sweenish

Diamond Member
I'm working on a class assignment that involves an array. I need to be able to put values in the array, but I'm getting two errors that I don't understand. It's likely you won't be able to compile because of the io.h, which is part of the software bundled with the textbook.

Code:
; Program Description: Finds the first 100 prime numbers, displays only the 100th
; Date: Apr. 2, 2015

.586
.MODEL FLAT

INCLUDE io.h            ; header file for input/output

.STACK 4096

.DATA
prime		WORD	2, 3, 98 DUP (?)
primeCount	BYTE	2
candidate	WORD	5
WindowLbl	BYTE	"The 100th prime number is:", 0
WindowBody	BYTE	6 DUP (?), 0

.CODE
_MainProc PROC
; while (primeCount < 100)
OuterWhileCheck:
cmp		primeCount, 100
jz		PrintPrime
; {
	; outer while loop body
	mov		cl, 1
	lea		ebx, prime

		; while (index <= primeCount && prime[index] % candidate != 0)
		InnerWhileCheck:
		cmp		cl, primeCount
		jnbe	ExitInnerWhile
		mov		edx, 0
		mov		eax, [ebx]
		div		candidate
		cmp		edx, 0
		jne		ExitInnerWhile
		; {
			; inner while loop body
			inc		cl
			add		ebx, 2
			jmp		InnerWhileCheck
			; end inner while loop body
		; }
		ExitInnerWhile:
		; if (index > primeCount)
                
                ; ********ERROR MARKED ON LINE BELOW***********
		cmp		cl, primeCount
		jna		EndOfIf
		; {
			; if body
			inc		primeCount
                        ; *******ERROR ON LINE BELOW*************
			mov		[ebx], candidate
			; end if body
		; }
		EndOfIf:

	add		candidate, 2
	jmp		OuterWhileCheck
	; end outer while loop body
; }

PrintPrime:
	wtoa	WindowBody, [ebx]
	output	WindowLbl, WindowBody
    mov     eax, 0  ; exit with return code 0
	ret
_MainProc ENDP
END

The io.h that I'm including just includes macros for easier input/output from the user. the output command and wtoa are the only commands that take advantage of it.

Error at line 50:
Error 2 error MSB3721: The command "ml.exe /c /nologo /Zi /Fo"Debug\example.obj" /W3 /errorReport😛rompt /Fl /Taexample.asm" exited with code 1.
C: \Program Files (x86)\MSBuild\Microsoft.Cpp\v4.0\V120\BuildCustomizations\masm.targets

Error at line 55:
Error 1 error A2070: invalid instruction operands C: \Users\Adam\OneDrive\Classes\CS 238\HW-DUE--04062015\windows32\windows32\example.asm

I'm a bit puzzled about what the issues are. Am I not able to move directly into a de-referenced address? And why can't I compare the cl register with candidate from memory?

These are the only two compile errors I have. I'd like to get them sorted to check for logic errors. I'm making this in Visual Studio 2013.

I know the 100th prime is 541, so I'm hoping it's not a size issue.
 
The problem is with this line:

Code:
            mov        [ebx], candidate
candidate is a memory location. You're trying to move from a memory location to another memory location, performing both a load and a store in the same instruction. That's not possible.

Try instead something like this:

Code:
            mov        eax, candidate
            mov        [ebx], eax

I don't know about the other error on line 50, the assembler output is useless. Try fixing this one and see if it's still there.
 
Forgot to consider I was pointing to memory.

Fixing that error gets it to compile clean, now I'll just have to deal with my logic errors. Thank you very much!
 
Back
Top