You are on page 1of 13

Simple Machine

CS270 Unit 3
Max Luttrell, Fall 2016
Simple Machine coding
• translate high-language to simple machine code

0x100A # LOAD SOURCE


LOAD SOURCE
dest = source; 0x200B # STORE DEST
STORE DEST
0x0000 # HALT

high-level simple hex


language machine instructions
(C) assembly (w/ comments)
copy1 program
#
# this first test case simply copies
# a data word from one location to another
#
# Algorithm: DEST = SOURCE
0x100A # (0)LOAD SOURCE - get word to copy
0x200B # (1)STORE DEST - put it where it goes
0x0000 # halt
# pad words: in case we want to add some instructions later
# we leave some room here.
0x0
0x0
0x0
0x0
0x0
0x0
0x0
0x40 # (0xA) SOURCE: - the word to copy
0x20 # (0xB) DEST: - the location to copy to
swap2 program
# this program exchanges two words A and B
# it needs a temporary location
#
# Algorithm:
# temp=a;
# a=b;
# b=temp;
#
# TEMP=a
0x1007 # load A
0x2009 # STORE TEMP - save it in temporary location
# A=B
0x1008 # (2)load B
0x2007 # STORE A - save it in A
# B=TEMP
0x1009 # LOAD TEMP - load temporary (old A)
0x2008 # STORE B -save it in B
0x0000 # halt
# data area follows
0x1234 # (7) A:
0x4321 # (8) B:
0x0 # TEMP:
indirection
• the Simple machine supports several
instructions for indirect addressing:
op-
instr name description
code
take memory address from
LA 0xB load address
instruction and load into accum
uses memory address found in
LIA 0xC load indirect
areg to load memory into accum,
address
i.e. accum = memory[areg]
stores value in accum into the
SIA 0xD store indirect
memory address in areg,
address
i.e. memory[areg] = accum
MVAA 0xE moves value in accum to areg
indirect program
#
# this test case simply copies a data word
# from one location to another using indirection.
# (it is based on the copy1 test case)
#
# Algorithm: *(DESTADDR) = SOURCE
0xB00B # LA DEST (move DESTADDR from ir to accum)
0xE000 # MVAA (move DESTADDR from accum to areg)
0x100A # LOAD SOURCE (read memory and store in accum)
0xD000 # SIA
0x0 # HALT
# pad words
0x0
0x0
0x0
0x0
0x0
0x40 # SOURCE (0xA): - the word to copy
0x20 # DEST (0xB): the destination
branching
• the Simple machine offers several instructions
for branching
op-
instr name description
code

MVAC 4 move to counter moves accum to ctr

JEQ 5 jump equal jump to address if ctr == 0

JLT 6 jump less than jump to address if ctr < 0

JMP 7 jump jump to address


C’s darkside:
goto’s and labels
• translate standard C if/else statement into ugly
C code using goto’s and labels
if (val1 > val2)
larger = val1;
else
larger = val2;

if (val2-val1 < 0)
goto val1gtr;
larger = val2;
goto done;

val1gtr:
larger = val1;

done:

larger program
#
# if (0 > (val2-val1)) goto val1gtr;
0x100a # (addr=0) load VAL1
0x900b # sub VAL2
0x4000 # mvac
0x6007 # (addr=3) jlt val1gtr
# larger=val2;
0x100b # load VAL2
0x200c # store LARGER
# goto done;
0x7009 # jmp done
# val1gtr: larger=val1;
0x100a # (addr=7)val1gtr: load VAL1
0x200c # store LARGER
# done:
0x0000 # halt
0xFFFB # (addr=0xA) VAL1:first operand
0xFFF0 # (addr=0xB) VAL2:second operand
0x0000 # (addr=0xC) LARGER
looping
• the Simple machine offers a counter register and
3 instructions which are helpful for looping

op-
instr name description
code

ADDC 3 add counter adds ctr to accum

MVAC 4 move to counter moves accum to ctr

DEC 0xA decrement counter ctr = ctr - 1


loop example
• translate the following C for loop into ugly C
code with goto’s and labels:
j = 0;
for (i=(N-1); i>=0; i--)
j += i;

j = 0;
i = (N-1); # store loop counter in i
goto check

again:
j += i;
i--;

check:
if (i<0) goto done;
goto again

done:

Exercise 3A

http://fog.ccsf.edu/~mluttrel/cs270/exercises/ex3a.html
Exercise 3B
1. examine the file sim/tests/addloop. note that the
comments are missing in more than half of the
program.

2. run the program and determine what it does.

3. make a copy of this program in your home directory


and fill in a comment on each line saying what the
line does.

4. play around with the number of elements in the array


and the element values and rerun the program. does
it still work?

You might also like