S2 assembly level tools set

S2 assembly language
S2 assembler
S2 simulator
Session example

S2 assembly language

The assembly language of s2 consisted of s2 ISA and pseudo command (.s, .a, .c, .w, .e).  In general, the input for the assembler is as follows: Comment preceeds by ;;  and takes effect to the end of line.  It is ignored by the assembler.

symbolic declaration

This section declares the binding of symbolic names to their value.  The value must be a numeric constant.
.s
temp 101
N 10
. . .

address setting

The absolute location for code and data can be set by
 
    .a  address


This command can appear in any section.  Usually it preceeds the code section and the data section.

code section

.a 0
.c
:label  add r1 r2 r3
 jmp always exit
. . .
:exit trap stop r0
Each line of code can be proceeded with a label (prefix with :) which its name can be used as an operand to an instruction.  An instruction is written is the form:

        opcode dest  source1  source2 . . .

The addressing mode can be expressed as:

ld   r1 temp      means   absolute address
ld   r1 @10 r2    means   ld  r1,10(r2)
ld   r1 +r2 r3    means   ld  r1,(r2+r3)
ld   r1 #n        means   load immediate

add  r1 r2 r3     means   r1 = r2 + r3
add  r1 r2 #10    means   r1 = r2 + 10

where all operands can be symbolic (prefix with null, @, +, #).

data section

Declare the constants or the variables used in the program.  Each symbol occupied one word.  The address of a variable can be declared using the lable.
.a 100
.w
10
20
temp
:ads variableA
ads  ;; this is the constant which is the address of variableA
The pseudo command .a sets the address.  All sections can be ordered in any sequences.  The last line of a program is .e

special command

The special commands are used to control the simulator: to stop, to print an integer value, and to print a character.
trap stop r0
trap print r1
trap printc r2

syntax

The assembler does not check for all possible illegal combination of opcode, addressing mode and operands.  The forms of assembly language for each S2 instruction are:
ld rd source
st source rd
aop rd rs1 rs2
aop rd rs #n
sop rd rs
jmp cond dest
jal rd dest
jr  rs
trap num rs
where
rd is r1..r31
rs is r0..r31
source -> absolute | disp | index | immediate    (as shown above)
aop -> add | sub | mul | div | and | or | xor    (ALU op)
sop -> shl | shr     (shift op)
cond -> always | eq | neq | le | lt | gt | ge    (conditional)
dest -> label | number

S2 assembler

        file.s2
          |
          V
         [as2] --> file.obj --> [s2] --> output (out.txt)
                   file.lis

An s2 assembly language program (file.s2) is a text file.  It is assembled into an executable file by a s2 assembler (as2). The output from the assembler is a loadable file format suitable for the s2 simulator (file.obj).  The assembler also produces a listing file (file.lis) for debugging purpose.  The listing shows where all labels are.   The predefined symbols in the assembler are:

        r0 .. r31, all opcodes, always, eq, neq, lt, le, ge, gt,
        stop, print, printc

The symbols are not case sensitive (they are all translated into uppercase inside the hash table).  The assembler does not rigorously check all validity of the assembly language input file.  The assembler produces the loadable file (file.obj) which the format is suitable to be used by the s2 simulator.

How the assembler work (link)

It is possible to add your own instruction, see the section "extended instruction" following the link "how the assembler work".  Note, you must modify the simulator appropriately to run your intructions.

S2 simulator

The simulator reads in an object code and controls its execution.  The interface to the command line is:

t               step, execute one instruction at the current pc
g              run from the current pc until it stops (by trap stop or a breakpoint)
b ads        set break point
c              clear all break points
d ads n      display the memory n words started from ads
r               display all registers
m a1 a2 n   move memory n words from a1 to a2.
                this can be used to fill the memory when a1+n and a2 are overlapped.
s  dest value  set the following destination:
   r0..r31
   m0..m20000
   pc, s, z
z 1/0        turn on/off the trace
o             display the output buffer
h             display summary of commands
q             quit   and store the output buffer to file "out.txt"

The memory is limited at 20000 locations. The usual interaction with the simulator is set the breakpoint to some line, execute the program to that point, inspect the values in register or memory, and continue.

 In the loop, the break point is usually set to some place within the loop, once it reaches there, to continue, uses step to step over that line (t) and continue (g), the program will go through the loop, coming back to the break point again.  Once the program stops the simulator will report the number of instruction executed and the CPI (calculated from the assumption about the number of cycle required by each instruction).  To get more measurement such as the frequency of use of each instruction, you have to modify the simulator (which can be done easily).

Session example

Let us use a program to illustrate how to use s2 assembler and simulator.  The assembly program adds 1..10.  The assembly language file is "add.s2".   The following command assembles the input file:

>as2 add.s2

The output are  "add.obj" and "add.lis" .  The listing file (add.lis) is shown below:

 
   0 ;; add 1 to 10
   0 ;; S store sum, I is 1..10
   0 .s
   0     S 100
   0     I 101
   0 .a 0
   0 .c
   0     st S r0        ;; s = 0
   1     ld r1 #1
   2     st I r1        ;; i = 1
   3 :loop ld r2 I
   4     sub r0 r2 #10
   5     jmp GT exit    ;; if i <= 10
   6     ld r1 S
   7     ld r2 I
   8     add r1 r1 r2
   9     st S r1        ;; s = s + i
  10     add r2 r2 #1
  11     st I r2        ;; i = i + 1
  12     jmp always loop
  13 :exit trap stop r0
  14 .e


The object file (add.obj) looks like this:

a 0
L 3 0 100
L 2 1 1
L 3 1 101
L 0 2 101
D 8 0 2 10
L 5 6 13
L 0 1 100
L 0 2 101
X 31 1 1 2 0
L 3 1 100
D 7 2 2 1
L 3 2 101
L 5 0 3
X 31 0 0 0 12
e
We start to test the program under s2 simulator:

>s2 add.obj

Now the object code is loaded.  We run the program step-by-step to the beginning of loop at location 3, using trace  (t):

load program, last address 14
>t
PC   0 st 100 r0   Z0 S0 R1:0 R2:0 R3:0 R4:0 R5:0 R6:0 R31:0
>t
PC   1 ld r1 #1    Z0 S0 R1:1 R2:0 R3:0 R4:0 R5:0 R6:0 R31:0
>t
PC   2 st 101 r1   Z0 S0 R1:1 R2:0 R3:0 R4:0 R5:0 R6:0 R31:0
>

and have a look at S and I at location 100, 101.  Dump from address 100, for 10 words.
>d 100 10
 100 : 0 1 0 0 0 0 0 0 0 0
 110 : >

now we can see that s = 0, and i = 1.  ( s is at 100, i is at 101).    We set the breakpoint at the loop (b 3), step over it, turn on the display (z 1), and go (g).  The execution stops when it hits the breakpoint again:

>b 3
>t
PC   3 ld r2 101        Z0 S0 R1:1 R2:1 R3:0 R4:0 R5:0 R6:0 R31:0
>z 1
>g
PC   4 sub r0 r2 #10    Z0 S1 R1:1 R2:1 R3:0 R4:0 R5:0 R6:0 R31:0
PC   5 jmp 6 13         Z0 S1 R1:1 R2:1 R3:0 R4:0 R5:0 R6:0 R31:0
PC   6 ld r1 100        Z0 S1 R1:0 R2:1 R3:0 R4:0 R5:0 R6:0 R31:0
PC   7 ld r2 101        Z0 S1 R1:0 R2:1 R3:0 R4:0 R5:0 R6:0 R31:0
PC   8 add r1 r1 r2     Z0 S0 R1:1 R2:1 R3:0 R4:0 R5:0 R6:0 R31:0
PC   9 st 100 r1        Z0 S0 R1:1 R2:1 R3:0 R4:0 R5:0 R6:0 R31:0
PC  10 add r2 r2 #1     Z0 S0 R1:1 R2:2 R3:0 R4:0 R5:0 R6:0 R31:0
PC  11 st 101 r2        Z0 S0 R1:1 R2:2 R3:0 R4:0 R5:0 R6:0 R31:0
PC  12 jmp 0 3          Z0 S0 R1:1 R2:2 R3:0 R4:0 R5:0 R6:0 R31:0
>

now i = 2 (in r2).  The next instruction will be at 3.  We can repeat the next loop by stepping over 3 and go until it comes back.  This is how the loop is traced.    Let run until the end of program.  Turn off the display (z 0) otherwise the display will be long (as it trace every intructions until the end).  Clear (c) the breakpoint and go (g).

>z 0
>c
>g
107 instructions, 565 clocks, CPI 5.28

We can see the result in r1  by displaying all registers (r) :

>r
r0 :0    r1 :55   r2 :11   r3 :0    r4 :0    r5 :0    r6 :0    r7 :0
r8 :0    r9 :0    r10:0    r11:0    r12:0    r13:0    r14:0    r15:0
r16:0    r17:0    r18:0    r19:0    r20:0    r21:0    r22:0    r23:0
r24:0    r25:0    r26:0    r27:0    r28:0    r29:0    r30:0    r31:0
>

To see the commands in the simulator use help (h), and we end the session here (q).
>h
g go, t single step, b set breakpoint, c clear break point
s set [rn,mn,pc,z,s], d dump a1 n, m move mem a1 a2 n
r register, z 1/0 display, o show output, q quit, h help
>q
 

last update 14 June 2003