COMP1521 22T1 - Week 02 Tutorial Answers
COMP1521 22T1 - Week 02 Tutorial Answers
COMP1521 22T1 - Week 02 Tutorial Answers
mipsy is very new so if you find bugs - please report in the course forum.
#include <stdio.h>
int main(void) {
int x, y;
scanf("%d", &x);
y = x * x;
printf("%d\n", y);
return 0;
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/02/answers 2/11
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Tutorial Answers
li $v0, 4
syscall
syscall
li $v0, 1
syscall
li $v0, 11
syscall
jr $ra # return
.data
prompt:
.asciiz "Enter a number: "
int main(void) {
int x, y;
scanf("%d", &x);
if (x > 46340) {
} else {
y = x * x;
printf("%d\n", y);
return 0;
ANSWER:
Please don't write C like this, unless you are translating it to assembler:
#include <stdio.h>
int main(void) {
int x, y;
scanf("%d", &x);
goto end;
square:
y = x * x;
printf("%d\n", y);
end:
return 0;
li $v0, 4
syscall
syscall
li $v0, 4
syscall
square:
mul $t1, $t0, $t0 # y = x * x
li $v0, 1
syscall
li $v0, 11
syscall
end:
jr $ra # return
.data
prompt:
.asciiz "Enter a number: "
too_big:
int main(void) {
int x;
scanf("%d", &x);
printf("medium\n");
} else {
printf("small/big\n");
Consider this alternate version of the above program, use its approach to produce simpler MIPS assembler.
#include <stdio.h>
int main(void) {
int x;
scanf("%d", &x);
message = "medium";
printf("%s", message);
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/02/answers 4/11
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Tutorial Answers
ANSWER:
Please don't write C like this, unless you are translating it to assembler:
#include <stdio.h>
int main(void) {
int x;
scanf("%d", &x);
printf("medium\n");
goto end;
small_big:
printf("small/big\n");
end:
return 0;
li $v0, 4
syscall
syscall
li $v0, 4
syscall
small_big:
li $v0, 4
syscall
end:
jr $ra # return
.data
prompt:
.asciiz "Enter a number: "
medium_str:
.asciiz "medium\n"
small_big_str:
.asciiz "small/big\n"
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/02/answers 5/11
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Tutorial Answers
li $v0, 4
syscall
syscall
end:
li $v0, 4
syscall
jr $ra # return
.data
prompt:
.asciiz "Enter a number: "
medium_str:
.asciiz "medium\n"
small_big_str:
.asciiz "small/big\n"
int main(void) {
printf("%d\n",x);
ANSWER:
Please don't write C like this, unless you are translating it to assembler:
#include <stdio.h>
int main(void) {
int x = 24;
loop:
printf("%d", x);
printf("%c", '\n');
x += 3;
goto loop;
end:
return 0;
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/02/answers 6/11
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Tutorial Answers
li $t0, 24 # i = 24;
loop: # loop:
li $v0, 1
syscall
li $v0, 11
syscall
end:
jr $ra # return
#include <stdio.h>
printf("*");
printf("\n");
};
return 0;
ANSWER:
Please don't write C like this, unless you are translating it to assembler:
// print a triangle
#include <stdio.h>
int i = 1;
loop0:
int j = 0;
loop1:
printf("%c", '*');
j++;
goto loop1;
end1:
printf("%c", '\n');
i++;
goto loop0;
end0:
return 0;
# i in register $t1
# j in register $t2
main:
li $t1, 1 # i = 1
loop0:
li $t2, 0 # j = 0
loop1:
li $v0, 11
syscall
j loop1
end1:
li $v0, 11
syscall
j loop0
end0:
jr $ra
#include <stdio.h>
int n;
printf("n = ");
scanf("%d", &n);
int fac = 1;
fac *= i;
return 0;
ANSWER:
Please don't write C like this, unless you are translating it to assembler:
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/02/answers 8/11
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Tutorial Answers
#include <stdio.h>
int n;
printf("n = ");
scanf("%d", &n);
int fac = 1;
int i = 1;
loop:
fac *= i;
i++;
goto loop;
end:
return 0;
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/02/answers 9/11
23/05/2022, 16:24 COMP1521 22T1 — Week 02 Tutorial Answers
main:
li $s0, 0 # n = 0
la $a0, msg1
li $v0, 4
li $v0, 5
### >>>>>
li $s2, 1 # fac = 1
main_fac_init:
li $s1, 1 # i = 1
main_fac_cond:
bgt $s1, $s0, main_fac_f # i <= n --> if (i > n) break
main_fac_step:
addi $s1, $s1, 1 # i++
j main_fac_cond
main_fac_f:
### <<<<<
la $a0, msg2
li $v0, 4
li $v0, 1
la $a0, eol
li $v0, 4
syscall # printf("\n")
jr $ra # return 0
.data
Revision questions
The following questions are primarily intended for revision, either this week or later in session.
Your tutor may still choose to cover some of these questions, time permitting.
https://cgi.cse.unsw.edu.au/~cs1521/22T1/tut/02/answers 11/11