blog

COMP1521 Week 3 CPU & MIPS

This week we touched on CPU architecture and MIPS Assembly language. And the lab last week is about floating points, which is very interesting.

CPU Architecture

A typical modern CPU has: (configuration differs)

Fetch-Execute Cycle

The CPU register keeps track of the program counter, and halts when reaches a halt signal. Some instructions may modify program counter, such as JUMP.
Example instruction encodings (not from a real machine)
Example instruction encodings (not from a real machine)
Each instruction involves:

MIPS Architecture

MIPS is a well-known and relatively simple architecture

MIPS CPU

Registers

Floating point registers

MIPS Assembly Language

# hello.s ... print "Hello, MIPS"

       .data          # the data segment
msg:   .asciiz "Hello, MIPS\n"

       .text          # the code segment
       .globl main
main:
        la $a0, msg   # load the argument string
        li $v0, 4     # load the system call (print)
        syscall       # print the string
        jr $ra        # return to caller (__start)

hello world

    .data
a:  .word 42            # int a = 42;
b:  .space 4            # int b;
    .text
    .globl main
main:   
    lw   $t0, a         # reg[t0] = a
    li   $t1, 8         # reg[t1] = 8
    add  $t0, $t0, $t1  # reg[t0] = reg[t0]+reg[t1]
    li   $t2, 666       # reg[t2] = 666
    mult $t0, $t2       # (Lo,Hi) = reg[t0]*reg[t2]
    mflo $t0            # reg[t0] = Lo
    sw   $t0, b         # b = reg[t0]

42 + 4

MIPS instructions

MIPS instructions
32 bits long

Region Address Notes
text 0x00400000 contains only instructions; read-only; cannot expand
data 0x10000000 data objects; readable/writable; can be expanded
stack 0x7fffefff grows down from that address; readable/writable
k_text 0x80000000 kernel code; read-only; only accessible kernel mode
k_data 0x90000000 kernel data; read/write; only accessible kernel mode

Fun stuffs

I was confused about the difference between asciiz and ascii. Doing some shallow searches, I know that they are both used to print ascii characters, but asciiz keeps printing until it meets a \0 whereas ascii automatically adds a \0 after your string. But I did not see differences by doing experiments. Doing deeper searches, I found that I used wrong method to experiment them. Initially I created one string and tried to see the different outputs between them. But later I found that there need to be something in after (e.g. another string) to let the differences appear. Because if there are only two string, using asciiz will stop printing anyway. But with another string, it will stop only when all strings are printed out. So asciiz should be used for most times to avoid unexpected behaviours.