AsmSim

Version 0.9a

(c)1996/97 Markus Hahn


Contents


Introduction

AsmSim is the shortcut for Assembler Simulator. It's an interactive applet for teaching and training you the basics of programming in assembly language. AsmSim is easy to use and understand and it's written in JAVA, so you can use it on every platform with a JAVA-enabled browser. 


How to use this applet

Compile

Compiles the actual instruction in Content to the address in Address. The Address will automatically be increased by 1.

Address

Here you put in the address where you want to compile a single instruction.

Content

Here you put in the instruction code if you want to compile a single instruction. Pressing the enter key will have the same effect as a click on the Compile button.

Memory

In the memory window you can watch the program and the data. When the program runs the actual PC will be marked if Animate PC is on.

Clear memory

You must click this button twice to clear the whole memory. All memory cells will be filled with the value 0.

Reset procesor

You must click this button twice to reset the processor. All registers will be set to zero, the stack pointer S to the highest address (currently 127).

Run

Starts the program. AsmSim executes the program at the max. possible speed. The label of the button changes to Stop so you can halt the program at every time you want. Click on Run again to continue the program then.

Single Step

Click on this button to execute only one instruction. Nice for teachers to describe a program's work or to for debugging purposes.

Editor

A simple editor where you can write the program code. The syntax for writing a line is 

ADDRESS: INSTRUCTION (OPERAND1(,OPERAND2))

Only one instruction per line is allowed. Comments must begin with a semicolon, e.g.

;this is a comment

Empty lines are skipped by the compiler.

Compile

Compiles the actual editor content to the memory. If you want to load already written programs use the copy+paste from your operating system. E.g. first write the program in a text editor, save it into a file and then copy the whole program to AsmSim's editor.

Copy memory

The content of the memory is copied with address headers to the editor. This allows you to save programs by transfering the editor content via copy+paste to an external editor. Remember that memory cells with the constant value 0 won't be copied to avoid unnecessary slack in the editor. If you want zero cells anyway you must add them by hand.

Animate PC

If you want the program counter (PC) to be shown in the memory window while the program is running you should activate this flag. If you don't like the flicker turn it off. This will also avoid scrolling.

Other controls

Registers are shown in the applet also as flags. All (error) messages are displayed in the Message Output, a kind of mini-terminal.


Technical background

This chapter explains the basics of programming in assembly language. It's still under construction so one or more explanations won't be very exact or might still lack some detail. Please mail me if you feel so.

Processor

A modern processor consists of a couple of registers among other things. Data registers (A and B) contain operands to perform operations like additions or comparisons on them. Address registers (F and G) usually contain addresses :) to allow data changes via pointer instruction. The stack register is used by instructions like POP or CALL to store values on the stack. The program counter PC (which can't be directly accessed by any instruction) contains the address of the actual executed instruction and is increased after every operation.

Memory

The memory is a one-dimensional array of cells/units. Every cell can either contain an instruction or a value. In the real world a basic cell contains one byte and values/instructions usually needs more than one byte to be stored. You should rather talk about addresses than about cells or memory units when working with AsmSim, of course.

Instructions

To tell a computer what to do you need a program (guess you already know that:). A program written in assembly language consists of single instructions which operate with the registers, the memory and the stack. Instructions have two, one or no operands. Operands contain information for the instructions, e.g. constants to add to a value into memory or an address to jump to. The syntax for instructions in AsmSim is always INSTRUCTION (OPERAND1(,OPERAND2))

Address modes

Operands can be declared in different modes. E.g. sometimes you might you want the result of an operation to be placed into a register, another time at a memory address. For that you can use 4 address modes in AsmSim:

Stack

The stack is an area in the memory defined by the stack pointer S. It's a first-in-last-out memory which is accessed by the instruction POP and PUSH (and indirectly by CALL and RET). The stack grows from the end to the beginning of the memory, so you have to watch if your program makes the stack too big (then it'll perhaps overwrite existing data or even instructions). The register S is automatically set to the end of the memory when the button "Reset processor" is clicked.

Number range

AsmSim works with signed 32bit integers only. So the valid range is - 2147483648 to 2147483647.


Reference

In the following you'll find an explanation for every instruction implemented in AsmSim. If an instruction has one or two operands you'll also find the possible address modes for them:

Here we go... 


NOP 

Does nothing (NOP = no operation), to be used as "dead code".

Operands: none


STOP 

Terminates the program. An exception will be shown although the execution of this instruction is not an error, of course.

Operands: none


RET 

The opposite instruction of CALL. The stack pointer S will be increased by 1, then the PC is loaded with the content of the address stored in S. This allows a subroutine to RETurn to its caller.

Operands: none


CALL 

Used to jump to a subroutine. The stack pointer S is decreased by 1, then the value PC+1 (the address of the first instruction following the CALL) will be written to the address placed in S. To return from a subroutine you should use the RET instruction.

Operand: mem


JMP 

The operand will be written to the program counter PC, so the next instruction will be executed at the new address, the program will JuMP.

Operands: mem/reg


JA 

Same as JMP, but will only occur when the carry flag is set to 1 and the zero flag is set to 0. Usually placed after a CMP instruction (JA = JUMP (if) ABOVE).

Operands: mem


JB 

Same as JMP, but will only occur when the carry flag is set to 0 and the zero flag is set to 0. Usually placed after a CMP instruction (JB = JUMP (if) BELOW).

Operands: mem


JE 

Same as JMP, but will only occur when the carry flag is set to 0 and the zero flag is set to 1. Usually placed after a CMP instruction (JE = JUMP (if) EQUAL).

Operands: mem


JC 

Same as JMP, but will only occur when the carry flag is set to 1 (JC = JUMP (if) CARRY).

Operands: mem


JO 

Same as JMP, but will only occur when the overflow flag is set to 1 (JO = JUMP (if) OVEFLOW).

Operands: mem


JZ 

Same as JMP, but will only occur when the zero flag is set to 1  (JZ = JUMP (if) ZERO).

Operands: mem


NEG 

Negates the operand (e.g. 22 will become -22).

Operands: mem/reg


ADD 

Adds the first operand to the second. If the summary is larger than the max. possible number the overflow flag will be set, otherwise it'll be cleared.

Operands: dir/mem/reg/ptr, mem/reg/ptr


SUB 

Subtracts the first operand from the second. The difference is written into the second operand. If the first operand was larger than the second one the overflow flag will be set, otherwise it'll be cleared.

Operands: dir/mem/reg/ptr, mem/reg/ptr


MUL 

Multiplies the first operand with the second. The product is written into the second operand. The high word of the result is written into the first operand. If the result is larger than the max. possible number the overflow flag will be set, otherwise it'll be cleared.

Operands: reg, mem/reg


DIV 

Divides the second operand by the first one. The result is written into the second operand.The modulo is written into the first operand. If the first operand equals zero the program will throw an exception.

Operands: reg, mem/reg


PUSH 

Writes to operand to the address defined in the stack pointer S, then decreases S by 1. Used to save values "on the stack".

Operands: dir/reg


POP 

Increases the stack pointer S by 1, then gets the value from the address defined in S and writes it to the operand. Used to get values "from the stack".

Operands: dir


MOVE 

Copies (the content of) the first operand into the second.

Operands: dir/mem/reg/ptr, mem/reg/ptr


CMP 

CoMPares the first (a) with the second (b) operand. If a<b then the zero flag is set to 0 and the carry flag is set to 1. If a>b zero is 0 and carry is 0. If a=b zero is 1 and carry is 0. Usually followed by a Jx instruction.

Operands: dir/mem/reg/ptr, mem/reg/ptr


DEC 

Decreases the operand by 1. If the operand gets smaller than zero the overflow flag is set to 1, otherwise to 0

Operands: mem/reg/ptr


INC

Increases the operand by 1. If the operand gets larger than the max. possible number the overflow flag is set to 1, otherwise to 0

Operands: mem/reg/ptr

-

-


Examples

The following two examples show you some capabilities of AsmSim. They're in ASCII text format. In a Windows 3.x/95/NT environment you can read them directly with the NOTEPAD.EXE program which should be associated with TXT files. Copy the complete source code out of an example and paste it to the editor, then compile and run it as described in the comments.


Future developments

There's still a lot to do, of course. Version 1.0 will include all popular binary-operating instructions like AND, OR, XOR, SHL, SHR, ROL, ROR and NOT. Another feature will be the possibility to display the register contents in hexadecimal and binary format. Constants will also be declarable in hex or binary.

Questions? suggestions? Don't hesitate, send an e-mail to the author at hahn@rz.fht-esslingen.de.

Sourcode? The whole project constists actually of about 2000 lines of code. It's not available at the moment (to avoid misuse), but will perhaps be when Version 1.0 is released.

Written by Markus Hahn

Last updated 18 Feb 97