[ Prev ] [ Index ] [ Next ]

Day 7

Created Tuesday 07 April 2020

Today we're gonna start going over more of the soft things of architecture.

MIPS Assembly Language. We use something called Mars. We'll be getting use to Mars and do stuff with it.

Since we're not gonna have another class for a while, he's gonna give us some extra homework that we can do ahead of time.

MIPS

What is an ISA? Interplay of C and MIPS ISA. Components of MIPS ISA

Coding today is a lot more structured. We're gonna learn about the wild west of programming from back in the day. COBOL and whatnot. A bit spaghetti code. People were getting their feet wet with the sophisticated software of today. Jump statements flying everywhere.

Stored Program Concept.
The stored program concept says that the program is stored with data in the coputer's memory. The computer is able to manipulate it as data — for example, to load it from disk, move it in memory, and store it back on disk.

This is the basic operating principle of every computer. So common we take it for granted. Without it, every instruction we initiated manually.

Computers went from simple programs to being much more advanced. The program we have could take up more memory that is available for example.

ISA (Instruction Set Architecture)

The ISA defines what instructions do ie. MIPS. Intel IA32 (x86), Sun SPARC, etc.
These are all ISAs.

ISAs we use to create a level of abstraction. We can specify what we want to get done and not have this depend on what the processing environment looks like.
ISA serves as an abstraction layer between the hardware and software. Before the ISA, the software was tied directly to the processor. This isn't good because if the CPU changes then the software needs to as well!

Running an Application

High Level Language Program
| compiler
V
Assembly Language Program
| Assembler
V
Machine Language Program
| Machine Language Interpretation
V
Control Signal Specification (High/Low on control lines. Yknow, the logic gates we use?)

C vs MIPS Programmers Interface

Registers: There are no Registers in C but there are in MIPS I ISA!
Memory: In C, we have local and global variables. In MIPS, we have 232 linear array of bytes.
Data Types: In C, we have int, short, char, etc. We also have aggregate data types (structures, arrays) and pointers. In MIPS, we have word(32b), byte(8b), half-word(16b), single FP(32b), double FP(64b).
Arithmetic Operators: C has +, -, *, /, etc. In MIPS, we have add, sub, mult, slt, etc.
Memory Access: a, *a, a[i], a[i][j] for C. But in MIPS we have lw, sw, lh, sh, lb, and sb.
Control: C has if, else, while, do-while, for, etc. MIPS has branches, jumps, and jump and link.

Load-Store ISA

There are two types of ISA. Load-Store is one of them. Memory Access (Load and Store between memory and registers). ALU (between registers).

MIPS is a register-to-register or load/store architecture. The desintation and sources must all be registers. Special instructions, we'll see later, are needed to access main memory.
MIPS uses three-address instructions for data manipulation.
Each ALU instruction contains a destination and two sources. For example, a = b + c has the form of: add a, b, c

Why Have Registers?

Memory-memory ISA

Benefits of registers

Load-Store ISA

Registers are a finite resource that needs to be managed. Compiler: register allocation

Goals

Issues

Components of an Instruction

The basic type of instruction has four components:

Btw, # is a comment!

add dst, src1, src2 # dst = src1 + src2
dst, src1, and src2 are register names ($)

C vs MIPS

In C, the line a = b + c makes sense. In MIPS, we use add $s1, $s2, $s3 where 1 is a, 2 is b, and 3 is c. However! a, b, and c are memory locations in C! s1, s2, and s3 are not memory but refers to the register!

What if we want to do something more complex like a = b + c + d - e?
We have to break it into multiple lines like so:
add $t0, $s1, $s2 # $t0 = b + c
add $t1, $t0, $s3 # $t1 = $t0 + d
sub $s0, $t1, $s4 # a = $t1 - e

In high level languages, we like to make those 'fancy exotic statements' as our professor puts it. Behind the scenes, it all gets broken down as seen above.

Often we want to be able to specify operand in the instruction: immediate or literal.

Use addi instruction
addi dst, src1, immediate
The immediate is a 16 bit signed value between -215 and 215-1
Signed-extended to 32 b its
Consider the following C code: a++;
The addi operator:
addi $s0, $s0, 1 # a = a + 1

Data Transfer Instructions

Data transfer instructions are used to move data to and from memory.
A load operation moves data from a memory location to a register and a store operation moves data from a register to a memory location.

Data transfer instructions have three parts:

The code looks like this: lw dst, offset(base)
Offset value is a signed constant

For example with C, a = b + *c;
We go to the address of C and add the value to b and store in a.
Use the lw instruction to load
Assume a($s0), b($s1), and c($s2)

lw $t0, 0($s2) # $t0 = Memory[c]
add $s0, $s1, $t0 # a = b + *c

This is all known as indirect addressing.
$s2 specifies a memory location and not a register! add is expecting three registers! We can't mix and match with something in memory (there are exceptions but we don't do it this way with MIPS if I caught that right).

Storing data is just the reverse and the instruction is nearly identical.
Use the sw instruction to copy a word from the source register to an address in memory

sw src, offset(base)
Offset value is signed

Arrays

Arrays are really pointers to the base addres in memory
Use offset value to indicate which index
Remember that addresses are in bytes, so multiply by the size of the element

Unlike C, assembly does not handle pointer arithmetic for you!

So then, take this C example: a = b + pow2[7]

Use the lw instruction offset, assume $s3 = 1000 and $s1 = b

lw $t0, 28($s3) # $t0 = Memory[pow2[7]]
add $s0, $s1, $t0 # a = b + pow2[7]


Now consider the C code: a[3] = b + c;

Use the sw instruction offset $s1 = b and $s2 = c

add $t0, $s1, $s2 # $t0 = b + c
sw $t0, 12($s0) # Memory[a[3]] = b + c

Branches

One of the distinguishing characteristic of computers is the ability to evaluate conditions and change control flow
In C, we have if-then-else, loops, and case statements.
In MIPS, we have

The simplest conditional test is the beq instruction for equality
beq reg1, reg2, label
Consider the code:
if (a == b) goto L1;
// Do something
L1: // Continue

Use the beq instruction
beq $s0, $s1, L1
# Do something

L1: # Continue

We also have bne which is not equal.
The j instruction jumps to a label.
j label

As an aside, yeah. Everything we do here is done in registers

Other Conditional Operators

In addition to == and !=, we have <, >, <=, and >=.
Register is 'set' to 1 when condition is met
Consider the following C code: if (f < g) goto Less;

slt $t0, $s0, $s1 # $t0 = 1 if $s0 < $s1
bne $t0, $zero, Less # Goto Less if $t0 != 0