Understanding Computer Architecture: MIPS Assembly and Memory

Sign Magnitude

  • The leftmost bit (most significant bit) represents the sign:
    • 1 – Negative
    • 0 – Positive
  • 00000101SM = 5
  • 10000101SM = -5

Two’s Complement

  • Positive numbers are identical to unsigned numbers.
  • To negate an unsigned number to its negative:
    • Go to the rightmost 1 and flip all bits to the left.
    • Example: -610 to (-1) * 01102 to 10102TC

Sign Extension

  • If unsigned/2TC is positive, add zeroes to fill out the byte.
  • If 2TC is negative, add ones to fill out the byte.
  • Generally, add whichever is the leftmost bit (most significant bit).

Overflow: An error condition that occurs when the result is larger than the number of bits available.

1-bit ; 4-bits = nibble ; 8-bits = byte

Word = 32-bits, 4-bytes ; Integer = 32-bits

Each memory address is 32-bits (1 word).

A memory address is 32-bits, and each address corresponds to 1 byte.

  • Each byte stores 1 byte of data.

If 0x00040010 stores 0xAABBCCDD, then:

Big Endian (MIPS & most)Little Endian (Intel)
0x00040010 contains 0xAA0x00040010 contains 0xDD
0x00040011 contains 0xBB0x00040011 contains 0xCC
0x00004012 contains 0xCC0x00040012 contains 0xBB
0x00004013 contains 0xDD0x00040013 contains 0xAA

Inside the CPU

Arithmetic Logic Unit + Registers + Program Counter + Controller

ALU: “Lizard Brain/Calculator”

Registers: Memory inside the CPU; MIPS has 32 x 32-bit registers.

Program Counter: Stores the address of the next instruction.

32 Registers in MIPS

$0 – $31

  • $0: Zero register, always zero.
  • $1: Reserved for the assembler.
  • $2, $3: $v0, $v1
  • $4-$7:
  • $8-$15: t-registers
  • $16-$23: s-registers
  • $24, $25: $t8, $t9
  • $26-$31:

Assembly Language Instructions

  • addi $R1, $R2, I # $R1 = Destination Register, $R2 = Source Register, I = Immediate (literal); $R1 = $R2 + I
  • li $R1, I # $R1 = I
  • add $R1, $R2, $R3 # $R1 = $R2 + $R3
  • sub $R1, $R2, $R3 # $R1 = $R2 – $R3

C++ Example

int v=0, w=1, x=2, y=3, z=4;
int main() {
  w = z;
  v = w + x;
  y = w - x;
  z = z - 1;
}

Assembly (ASM) Equivalent

# v is $s0
# w is $s1
# x is $s2
# y is $s3
# z is $s4
.data # "From here on store as data"
.text # "From here on it is CODE"
main:
  li $s0, 0
  li $s1, 1
  li $s2, 2
  addi $s3, $0, 3 # Uses addi instruction and $0 (always zero)
  addi $s4, $0, 4 # Register to add literal to respective register (same effect as li)
  add $s1, $s4, $0 # w=z
  add $s0, $s1, $s2 # v = w+x
  sub $s3, $s1, $s2 # y = w-x
  add $s4, $s4, -1 # z = z-1
  li $v0, 10 # System call for exit
  syscall # }

System Calls (SYSCALL)

  • syscall 1: Print immediate to console; the value to print is in $a0.
# n is $s0
.data
.text
main:
  li $s0, 5 # int n=5;
  li $v0, 1 # cout << n;
  move $a0, $s0
  syscall
  li $v0, 10 # } Terminate program
  syscall

  • syscall 4: Print string to console; the address of the string to be printed is in $a0.
endl: .asciiz "\n" # asciiz means 0-terminated string
.text
main: li $v0, 4 # cout << endl;
      la $a0, endl

  • syscall 5: Read an integer; input appears in $v0.