blog

COMP1521 Week 5 MIPS & syscall

This week dives much deeper into MIPS and system calls.

Structs in MIPS

Structs in MIPS

Struct members are accessed by offset.

// new type called "struct _student"
struct _student {...};
// new type called Student
typedef struct _student Student;
                  // sizeof(Student) == 56
stu1:             Student stu1;
   .space 56
stu2:             Student stu2;
   .space 56
stu:
   .space 4       Student *stu;

To define a struct in MIPS, space is used and sizeof is used to determine the actual space used.

stu1: .space 56       # Student stu1;
stu2: .space 56       # Student stu2;
# stu is $s1          # Student *stu;

li  $t0  5012345
sw  $t0, stu1+0       # stu1.id = 5012345;
li  $t0, 3778
sw  $t0, stu1+44      # stu1.program = 3778;

la  $s1, stu2         # stu = &stu2;
li  $t0, 3707
sw  $t0, 44($s1)      # stu->program = 3707;
li  $t0, 5034567
sw  $t0, 0($s1)       # stu->id = 5034567;1
# Student stu; ...
# // set values in stu struct
# showStudent(stu);

   .data
stu: .space 56
   .text
   ...
   la   $t0, stu
   addi $sp, $sp, -56    # push Student object onto stack
   lw   $t1, 0($t0)      # allocate space and copy all
   sw   $t1, 0($sp)      # values in Student object
   lw   $t1, 4($t0)      # onto stack
   sw   $t1, 4($sp)
   ...
   lw   $t1, 52($t0)     # and once whole object copied
   sw   $t1, 52($sp)
   jal  showStudent      # invoke showStudent()
   ...

pass struct by value

To pass whole structure to functions, push the structure onto stack.

Computer Systems Architecture

architecture

Operating systems provide an abstraction layer on top of hardware. Some characteristics of OSs:

There are different types of OSs:

Fun stuffs

There is a system call open (man 2 open) defined in <fcntl.h>. There is also a command called open in macOS. I aws confused about these two commands and thought typing open would trigger the system call. They are really confusing because their names are same. After searching around, I found that macOS does not provide a direct syscall like open, rather it just tries to open the file using installed application.