0

I have a simple program that adds 1+1 until it reaches a million, and then it prints "Done!" to the console. But when it runs, it does nothing. Here is the code:

.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
          main dd 0
          msg  db "Done!", 0
.code      
start:      
_loop:    mov eax, 1 
          mov main, eax
          cmp main, 1000000
          jz _next
          jmp _loop
_next:    invoke StdOut, addr msg
          invoke ExitProcess, 0
end start

It doesn't do anything when I run it. I don't see why. I currently run it through cmd, by clicking and dragging it to the prompt and pressing enter. And also, is MASM32 the easiest assembler to learn, or are there better and easier ones out there? If so, could someone post the ones that are better? Thanks. I just need somewhere to start learning x86 assembly.

Thanks in advance,

Progrmr

Progrmr
  • 1,575
  • 4
  • 26
  • 44
  • I doubt that you'll find any other assembler appreciably easier to use. What you're struggling with is x86 assembly language, not the assembler program itself. That said, some people prefer NASM (http://www.nasm.us/), over MASM. – Jim Mischel Apr 19 '12 at 06:37

3 Answers3

5

Your program doesn't increment anything. You probably want an add instruction in there somewhere.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
1

There are various errors. For clarity and other users reading this post...

EAX is being set to 1 on each loop:

_loop:
mov eax, 1         ;eax set to 1

No need to mov the value in eax - test against eax directly.

mov main, eax

The OP wants to increment by 1 each time therefore the missing instruction is "inc eax" - no "add" needed:

start:
xor eax,eax     ;eax=0
_loop:          ;Begin loop
  inc eax         ;Increment eax by 1 each loop
  cmp eax,1000000 ;Compare
  je _next        ;Done - exit loop
  jmp _loop       ;Continue loop

_next:
...
JBES
  • 1,512
  • 11
  • 18
0
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\masm32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\masm32.lib
.data
    main dd 0
    msg  db "Done!", 0
.code      
start:      
    mov     eax,1               ;set eax register to 1
IncreaseBy_1:
    inc     eax                 ;increase eax by 1
    cmp     eax,1000000         ;compare eax to 1000000
    jnz     IncreaseBy_1        ;jump if eax not equal to 1000000 
_next:    
    invoke  StdOut,addr msg
    invoke  ExitProcess,0
end start
honk
  • 9,137
  • 11
  • 75
  • 83
pxor
  • 1
  • It would be nice if you could add some explanation on why your solution works in contrast to the code of the OP. – honk Sep 25 '15 at 20:29