News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

zep2 simulator

Started by vivendi, September 21, 2006, 05:44:35 PM

Previous topic - Next topic

vivendi

Hello, i've just started with learning ASM at school, which is fun and all, only the down side is, is that we use a simulator for our ASM code and we dont use it on a real processor like Intel (80x86).
The simulator is called "Zep2 Proccesor simulator", i dont think anyone here ever heard about it. I think its only used here, in the netherlands, thats where im from.

But we are also allowed to use ASM for the 80x86 processor, so i've choosed to do that, but all i need to do now is to port the code that i have for the Zep2 simulator to MASM code. And thats where i need a little help.

I'll show you a piece of code that the Zep2 uses and ill try to explain what it does, R1 and R2 are  the only registers that are availible.


##

begin load R2, -2;  //laad R2 met -2 -> Load R2 with -2

      load R1,R2;   //inhoud R2 naar R1 -> content of R2 to R1

      inc R1;       //tel 1 op bij inhoud R1 -> add +1 to R1

      store R1,0xf; //bewaar inhoud R1 op adres 0xf -> Save content of R1 to address 0xf

      nop;          //doe niets (no operation) -> no operation

      halt;        //end of program..?

##


Dutch comment on the left (you can ignore that), english comment on the right.
Now this is a program we had to write, nothing special as you can see.
But i wanted to change this code so it can be assembled with a MASM compiler.
Im not sure but i think the 'load' command here is the equivalent of 'mov' in 'real' ASM.

So my question is, how would this piece of code look like if it was written with MASM...???

mnemonic

Hi vivendi,

welcome on board.

The thing you want us to accomplish suspiciously looks like homework to me, so you will have to come up with some code yourself and we will help you get out of the trouble. That is what the rules say.

Anyway, I assume that you are a total beginner and I will help you with the first steps.

As you already realized the "load" instruction is "mov" on x86. "store" is "mov" too.
Have a look at the file opcodes.hlp that you will find in the \masm32\help\ directory, there is a good listing of mnemonics (instructions) and a explanation for each one.
That should be good to keep you going.

I will provide you with some basic layout code that will assemble fine and does nothing except from terminating.
include \masm32\include\masm32rt.inc

.data
; data goes here

.code
start:
; your code goes here
invoke ExitProcess,NULL ; terminate program
end start


You can use it as a skelleton for your code and play around.

If you are stuck and need additional help, you have to show off some x86 code that you wrote yourself, so we can give you further help.

Good luck :U

Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

vivendi

Well, its not really a homework assignment yet  :P
Im not using MASM, but im using the masm linker, i got that tool with a book i once bought about ASM, but never really read the book untill now.
So i dont have the help files and the include files.
But i should be able to play around with this piece of code right?


.386
.model small
.stack
.data

.code

main proc
    //code goes here...

main endp
end


This is what i have so far.


.386
.model small
.stack
.data

.code

main proc

mov eax, -2
mov ebx, eax
inc eax
mov eax, 0xf
nop

main endp
end


But im getting an error when i try to assemble this. I thinik its about this line: "mov eax, 0xf"

Error:
Quote
Microsoft (R) Macro Assembler Version 6.11d
Copyright (C) Microsoft Corp 1981-1995.  All rights reserved.

Assembling: opdr2.asm
opdr2.asm(13) : error A2206: missing operator in expression

mnemonic

Hey, nice. :clap:

Ok, then...

Add the following line between .data and .code:
myvar dd 0 ; reserves a doublword for data storage

And then replace
mov eax, 0xf
with
mov myvar, eax ; copy contents of eax to myvar which is actually an address in memory
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

mnemonic

Oh, I overlooked the actual error...

You may not use the "0x" prefix to tell MASM that you mean a hex value. For that purpose simply put a "h" at the end of the value, e.g. "0Fh".
For binary values you would put a "b" at the end, e.g. "011011110001b".

Anyway, even "mov eax, 0Fh" would not have been done what you expected. That means: Move the value 0Fh into EAX register.
Intel mnemonics always follow the pattern: "Instruction Target, Source".
Be kind. Everyone you meet is fighting a hard battle.--Plato
-------
How To Ask Questions The Smart Way

vivendi

Ok, so i changed that line to this now:

mov 0fh, eax

But now im getting the following error:
Quote
opdr2.asm(13) : error A2001: immediate operand not allowed

I tried to google it, but i couldn't find any usefull solutions. So i have no idea how to fix this.

ninjarider

you trying to move a value to an immediate. which you cannot do.

mov 0fh, eax is not allowed.

if your trying to move the 0fh value into eax use this.
mov eax, 0fh

QuoteIntel mnemonics always follow the pattern: "Instruction Target, Source".

you could also say it as. Instruction Destination, Source.

from what I see in your code your trying to store a value into memory. theres a few ways of doing that.

declare a variable in the data section. lets say you declare Var1
you would use this to store your information

mov Var1, eax

the other way is by using a pointer to the memory location.

mov byte ptr [0fh], al
this tells masm that you want to store the value of the al register at the offset of 0fh
not sure but i think for eax it would be
mov dword ptr [ofh], eax

hope this clears things up for you.

also your using the store instruction. there is a stosb, stosw, stosd in assembly.
when using these instructions make sure that (e)di has the value of your offset and es equals your segment.
note that this instruction with incriment the value of (e)di as needed.

vivendi

Thanks alot for your explanation! I've found out that its better not to assign a value directly to a memory address, since this is pretty uncomon, unless you really know what you're doing.
So instead its better to create a variable which automatically gets a memory address assigned.

I have this piece of code now, which should work if im not mistaking.


.386
.model small
.stack
.data
my_var dd 0

.code

main proc

mov eax, -2
mov ebx, eax
inc eax
mov dword ptr[my_var], eax
nop

main endp
end


It assembles fine but i get this warning:

Quote
LINK : warning L4038: program has no starting address

Any idea what i have to do to fix this..?? And if you see any mistakes in my code, please let me know.

sinsi

The "end" directive in a program usually tells the linker where the program starts, so use
   end main
This will set the proper entry point for your .exe
Light travels faster than sound, that's why some people seem bright until you hear them.

ninjarider

Quote.386
.model small
.stack
.data
   my_var dd 0

.code

main proc
[/qoute]
START:
[qoute]

   mov eax, -2
   mov ebx, eax
   inc eax
   mov dword ptr[my_var], eax
   nop

main endp
END START

you need the START: to let the assembler know were your wanting your program to start

also you do not need to have mov dword ptr [myvar], eax. just use mov myvar, eax.
mov myvar, eax stores the value of eax into the memory address for myvar

using the mov dword ptr [myvar], eax. this takes the value of eax and puts it in the memory location pointed to be myvar which is 0. which could result in really bad things.

vivendi

ahh i see, well i guess i've got all the answer i needed for this question, so i thanks you all who helped me with this!! :)
Im gonna play around with ASM and try to do some exercise's.