#// Written by Hammond Pearce ##include PRINT_STR=4 PRINT_INT=1 #struct pizza_t { # char size[10]; # int price_cents; #}; #struct pizza_t pizza_options[3] = { # {"small", 300}, # {"medium", 550}, # {"large", 800} #}; PIZZA_SIZE_OFFSET=0 PIZZA_PRICE_OFFSET=PIZZA_SIZE_OFFSET+10+2 PIZZA_STRUCT_SIZE=PIZZA_PRICE_OFFSET+4 .data pizza_options: .asciiz "small" .space 10-6 .align 2 .word 300 .asciiz "medium" .space 10-7 .align 2 .word 550 .asciiz "large" .space 10-6 .align 2 .word 800 ###################################### # Strings options_header: .asciiz "The available pizza options are:\n" size_field: .asciiz "Size: " size_sep: .asciiz ", " price_field: .asciiz "price: " price_units: .asciiz " cents\n" ####################################### # The code #void print_pizza_t(struct pizza_t *pizza); #void increase_price(struct pizza_t *pizza, int increase_cents); # .text main: #int main(void) { main__prologue: begin push $ra push $s0 # stores pizza option in question push $s1 # stores main__loop index main__body: li $v0, PRINT_STR la $a0, options_header syscall # printf("The available pizza options are:\n"); # i in $s1, tmp in $t1 # int i; main__loop_init: li $s1, 0 # i = 0; main__loop_cond: bge $s1, 3, main__loop_end # if (i >= 3) goto main__loop_end; main__loop_body: mul $s0, $s1, PIZZA_STRUCT_SIZE # struct pizza_t* tmp = &pizza_options[i]; add $s0, $s0, pizza_options move $a0, $s0 li $a1, 100 jal increase_price # increase_price(tmp, 100); move $a0, $s0 jal print_pizza_t # print_pizza_t(tmp); main__loop_step: addi $s1, $s1, 1 # i++; b main__loop_cond # goto main__loop_cond; main__loop_end: li $v0, 1 # return 0; main__epilogue: pop $s1 pop $s0 pop $ra end jr $ra print_pizza_t: #void print_pizza_t(struct pizza_t *pizza) { move $t0, $a0 li $v0, PRINT_STR la $a0, size_field syscall # printf("Size: "); addi $a0, $t0, PIZZA_SIZE_OFFSET syscall # printf("%s", pizza->size); la $a0, size_sep syscall # printf(", "); la $a0, price_field syscall # printf("price: "); li $v0, PRINT_INT lw $a0, PIZZA_PRICE_OFFSET($t0) syscall # printf("%d", pizza->price_cents); li $v0, PRINT_STR la $a0, price_units syscall # printf(" cents\n"); jr $ra increase_price: #void increase_price(struct pizza_t *pizza, int increase_cents) { lw $t0, PIZZA_PRICE_OFFSET($a0) add $t0, $t0, $a1 sw $t0, PIZZA_PRICE_OFFSET($a0) # pizza->price_cents += increase_cents; jr $ra