Peter Mawhorter
Short answer: the compiler handles this for us.
The schematic diagram of the full CPU.
The CPU connections listed as a table:
Component | Inputs | Outputs |
---|---|---|
PC |
|
|
Instruction Memory |
|
|
Control Unit |
|
|
Register File |
|
|
Arithmetic Logic Unit (ALU) |
|
|
Data Memory (RAM) |
|
|
BEQ Logic |
|
|
Assembly Syntax | Meaning (R = register file, M = data memory) |
Opcode | Rs | Rt | Rd |
---|---|---|---|---|---|
ADD Rs, Rt, Rd | R[d] ← R[s] + R[t] | 0010 | s | t | d |
SUB Rs, Rt, Rd | R[d] ← R[s] - R[t] | 0011 | s | t | d |
AND Rs, Rt, Rd | R[d] ← R[s] & R[t] | 0100 | s | t | d |
OR Rs, Rt, Rd | R[d] ← R[s] | R[t] | 0101 | s | t | d |
LW Rt, offset(Rs) | R[t] ← M[R[s] + offset] | 0000 | s | t | offset |
SW Rt, offset(Rs) | M[R[s] + offset] ← R[t] | 0001 | s | t | offset |
BEQ Rs, Rt, offset | If R[s] == R[t] then PC ← PC + 2 + offset * 2 |
0111 | s | t | offset |
JMP offset | PC ← offset * 2 | 1000 | offset | ||
HALT | Stops the program | 1111 | ignored |
if
/while
/catch
/etc. ≈ BEQ ±
JMPThings you should be able to do in your favorite editor:
Bonus: do all of these without moving your hands off the home row.
printf
#include <stdio.h>
gets
you printf
Prints:
%
creates a hole, letter specifies what kind
DON’T BE FOOLED
IN C THERE ARE ONLY POINTERS
Assembly Syntax | Meaning (R = register file, M = data memory) |
Opcode | Rs | Rt | Rd |
---|---|---|---|---|---|
ADD Rs, Rt, Rd | R[d] ← R[s] + R[t] | 0010 | s | t | d |
SUB Rs, Rt, Rd | R[d] ← R[s] - R[t] | 0011 | s | t | d |
AND Rs, Rt, Rd | R[d] ← R[s] & R[t] | 0100 | s | t | d |
OR Rs, Rt, Rd | R[d] ← R[s] | R[t] | 0101 | s | t | d |
LW Rt, offset(Rs) | R[t] ← M[R[s] + offset] | 0000 | s | t | offset |
SW Rt, offset(Rs) | M[R[s] + offset] ← R[t] | 0001 | s | t | offset |
BEQ Rs, Rt, offset | If R[s] == R[t] then PC ← PC + 2 + offset * 2 |
0111 | s | t | offset |
JMP offset | PC ← offset * 2 | 1000 | offset | ||
HALT | Stops the program | 1111 | ignored |
OR R1, R1, R2
)
andLW R2, 1(R1)
)…
*
operator to dereference it and talk
about what’s at that address (can both read & write there)&
operator to get the address of something
which can be stored in a pointerint *p
, p + 1
is 4 bytes
farther)void *
when we don’t know what we’re pointing
toint vec[]
is really just int *vec
char *
Prints:
Declare a variable x
of type “array-of-int”
Actually: type int *
, or
pointer-to-int
Put the numbers 1, 2, 3 into that array
Actually: allocate space for three numbers as part of
the program, and store the address of the first one in
x
Prints (%p
is the format for a pointer):
This is the memory address of the first number
Prints:
??? Addresses change each time you run it, for security
Prints:
*(x + 1)
??? Add 1 to address of x, get value there
int x[]
is really a
pointer int *x
[]
means unspecified size.x[2]
is really just
*(x + 2)
malloc
to ask for some bytesmalloc
sizeof
to figure out # of bytes based on # of
entries of a specific typevoid *
, we cast to what we need(type) value
)char *string = "Some words."
?char *string
char *
0x00
at end.
string.h
gdb
and valgrind
gdb
stands for “Gnu DeBugger”valgrind
after the gates of Valhalla
(“grinned,” not “grind”)gdb
gdb
to debug a C program compiled with -g:
valgrind
valgrind
‘memcheck’ mode to check for memory errors
during execution: