# access fields of a simple struct # struct details { # uint16_t postcode; // Size = 2 bytes, Offset = 0 bytes # uint8_t wam; // Size = 1 byte , Offset = 2 bytes # // Hidden 1 byte of "padding" # // Becase the Offset of each field must be a multiple of the Size of that field # uint32_t zid; // Size = 4 bytes, Offset = 4 bytes # }; // Total Size = 8 # // The Total Size must be a multiple of the Size of the largest field in the struct # // More padding will be added to the end of the struct to make this true # // (not needed in this example) # offset in bytes of fields of struct details OFFSET_POSTCODE = 0 OFFSET_WAM = 2 OFFSET_ZID = 4 # unused padding byte before zid field to ensure it is on a 4-byte boundary main: ### Save values into struct ### la $t0, student # student.postcode = 2052; addi $t1, $t0, OFFSET_POSTCODE li $t2, 2052 sh $t2, 0($t1) la $t0, student # student.wam = 95; addi $t1, $t0, OFFSET_WAM li $t2, 95 sb $t2, 0($t1) la $t0, student # student.zid = 5123456 addi $t1, $t0, OFFSET_ZID li $t2, 5123456 sw $t2, 0($t1) ### Load values from struct ### la $t0, student # printf("%d", student.zid); addi $t1, $t0, OFFSET_ZID lw $a0, 0($t1) li $v0, 1 syscall li $a0, ' ' # putchar(' '); li $v0, 11 syscall la $t0, student # printf("%d", student.wam); addi $t1, $t0, OFFSET_WAM lbu $a0, 0($t1) li $v0, 1 syscall li $a0, ' ' # putchar(' '); li $v0, 11 syscall la $t0, student # printf("%d", student.postcode); addi $t1, $t0, OFFSET_POSTCODE lhu $a0, 0($t1) li $v0, 1 syscall li $a0, '\n' # putchar('\n'); li $v0, 11 syscall li $v0, 0 # return 0 jr $ra .data student: # struct details student; .space 8 # 1 unused padding byte included to ensure zid field alligned on 4-byte boundary