pletcha@archimedes Proj1

Download Report

Transcript pletcha@archimedes Proj1

Modifying the Standard Instruction Set
Save everything:

Make sure that the directory that contains has, mas and sim
belong to your path
[pletcha@archimedes asm]$ echo $PATH
bin:/usr/sbin:.../home/pletcha/Courses/asm:/home/pletcha/bin

Make a tarball of all standard instruction set files, copy it to a
working directory and expand the tarball:
$ cd <working directory>
$ tar cf standard.tar s.*
$ mkdir StandardModifications/Proj1
$ cp standard.tar StandardModifications/Proj1
$ cd StandardModifications/Proj1
$ tar xf standard.tar
What things should look like:
[pletcha@archimedes Proj1]$ ls
s.cfg s.has s.hor s.hst standard.tar s.txt s.vas s.ver s.vst
[pletcha@archimedes Proj1]$
Files we need to change:


s.cfg: This file contains a list of all pertinent assembly
language instructions and the CPU configuration – number of
CPUs,
s.has: files containing the course code for all the assembly
language instructions in the Standard Instruction Set.
Example:



Prepare a special register for saving the stack pointer and two
new instructions that save and recover the stack pointer:
sav: This saves the stack register to the new sv register.
rec: This puts the value of the stack pointer stored in sv back
into the sp register.
s.cfg
30 swap 0
; mnemonic and number of operands for opcode F7
31 sav 0
; mnemonic and number of operands for opcode F8
32 rec 0
; mnemonic and number of operands for opcode F9
33
34
35 %%
; Part 2 (reg names in number order starting with reg 6)
36
37 pc
; register 6
38 sp
; register 7
39 ac
; register 8
40 ir
; register 9
41 dc
; register A
42 sv
; register B - used for saving the stack pointer (sav & rec instructions)
s.has

First we notice that the decoding is already done for us for the
new instructions we will write.
47 LF_1:
48 LF_10:
49 LF_100:
50
dc = left(dc); if (s) goto LF_11;
dc = left(dc); if (s) goto LF_101;
dc = left(dc); if (s) goto LF_9;
goto LF_8;
Now we code at labels LF_8 and LF_9:
/***************************************************************/
/
Available Opcodes
/
/***************************************************************/
LF_8:
/------------------------ SAV ----------------------sv = sp; goto fetch;
LF_9:
/------------------------ REC ----------------------sp = sv; goto fetch;
LF_A:
/---------------------------------------------------LF_B:
/----------------------------------------------------
Testing our new Instructions:

First we need to save the new instruction set in a tar ball and
then copy the contents of the tarball to the working directory.
[pletcha@archimedes Proj1]$ ls
s.cfg s.has s.hor s.hst standard.tar s.txt s.vas s.ver s.vst
[pletcha@archimedes Proj1]$ tar cf sav_rec.tar s.*
[pletcha@archimedes Proj1]$ ls
sav_rec.tar s.cfg s.has s.hor s.hst standard.tar s.txt s.vas s.ver s.vst
[pletcha@archimedes Proj1]$ tar tf sav_rec.tar
s.cfg
s.has
s.hor
s.hst
s.txt
s.vas
s.ver
s.vst
[pletcha@archimedes Proj1]$ grep sav s.cfg
sav 0
; mnemonic and number of operands for opcode F8
sv
; register B - used for saving the stack pointer
; (sav & rec instructions)
What next:



Remember the program shown in Chapter 4 where we
swapped out the stack pointer and made it point to an array of
numbers. We then added up all the numbers.
We will execute the same program again but this time we will
use sav and rec to save the stack pointer.
Let's call this version of the program sav_rec.mas
!s
; needed to ensure we use s.hor and s.cfg
sav
; save the stack pointer
ldc
table
swap
ld
count
ja
test
loop: ld
sum
addr
0
st
sum
dloc
1
; sp now points to &table[i]
ld
count
sub
@1
st
count
msg:
dw
test: jnz
loop
@1:
dw
ldc
msg
count: dw
sout
sum:
dw
ld
sum
table: dw
dout
dw
-8
ldc
'\n'
dw
444
aout
...
rec
; recover the stack pointer
halt
“sum = “
1
10
0
56
Assembling and Running the new Program:



Just as before we use mas to assemble and sim to run.
This time, run from inside sim and when all done print out the
registers – r
You'll see the following:
12: rec /F9 00/ sp=000A/0000
13: halt /FFFF /
Machine inst count = 5D (hex) =
---- [T7] r*
pc = 0014 sp = 0000 ac
93 (dec)
= 000A
even though we know sp was swapped out and used to step
through the list of numbers.
Lab Exercise:




In all versions of the table sum exercise we use the ac register
for three purposes – sum up the entries in the table, check
jump condition and decrement the counter variable.
Let's start by changing how we test the jump condition.
We will introduce a new index register and have the jnz
instruction jump on the new register – ct – being non-zero.
Create a new register called ct.
Lab Exercise:
37 %%
38
39 pc
40 sp
41 ac
42 ir
43 dc
44 sv
45 ct
; Part 2 (reg names in number order starting with reg 6)
; register 6
; register 7
; register 8
; register 9
; register A
; register B - used for saving the stack pointer (sav & rec instructions)
; register C - used for counting in loops, etc
Lab Exercise:


We need to look at the microcode of all the jump instructions
except ja. They all involve passing the ac register through the
ALU and then testing to see if the z-flag is set.
We can replace this by replacing the command 0 = ac; with
0 = ct; Now we don't care which registers were involved in the
LD:
/------------------------- JNZ ---------------------/ 0 = ac; if (z) goto fetch;
0 = ct; if (z) goto fetch;
pc = ir & xmask; goto fetch;
previous operation that sent data through the ALU.
Lab Exercise:

Finally, we need our special index register with its own
decrement instruction.

Start by modifying the s.cfg file.

We'll need two new instructions

–
init: with 0 arguments, copies the contents of ac into
ct
–
dec: with 0 arguments, decrements ct by 1
Add both of these to s.cfg.
Lab Exercise:
30 swap 0
31 sav 0
32 rec 0
33 init 0
34 dec 0
35
36
; mnemonic and number of operands for opcode F7
; mnemonic and number of operands for opcode F8
; mnemonic and number of operands for opcode F9
; mnemonic and number of operands for opcode FA
; mnemonic and number of operands for opcode FB
Lab Exercise:



Now we need to add the new instructions to s.has and rebuild
s.hor.
Now it is time to produce a new copy of the original program
that uses the new instructions – sav, rec, init and dec as well
as the new version of jnz.
Once your new program is working properly and adds the
numbers to 344, save everything.
Proj 1$ tar cf sav_rec.tar s.* sav_rec.mas
More Lab Exercises:

Stat a new project by creating a new project directory –
StandardModifications/Proj2

Copy standard.tar this directory and expand it

Complete Problem 6.19

Start up a new project - Proj3 – and implement Problem 6.20

In implementing 6.20 we need to remember that inside the
execute part of the instruction (after fetch and decode) we
need to fetch again twice; once to get the address and once to
get the value stored at that address.