Student name: Nguyễn Hoàng Long
Student number: BI11-157
Class: A1
Computer Architecture
1. Given the following program.
.text
.globl main
main:
li $a0, 5 \\ set $ao to 5
jal function \\ jump to function(save the return address)
move $a0, $v0
li $v0, 1
syscall
li $v0, 10
syscall
function:
move $t0, $a0 \\ move the value of $a0 to $t0
li $t1, 0 \\ set $t1 to 0
loop : beq $t0, 0, end {if $t0 == 0, jump to end
add $t1, $t1, $t0 $t1 = $t1+$t0
sub, $t0, $t0, 1 $t0 = $t0 -1
b loop \\ jump to loop}
end: move $v0, $t1 {move the value of $t1 to $v0
jr $ra jump back to the saved address}
a) What is the value printed by this program (first system call in the
program).
The value printed is 15
b) If register $a0, used for argument passing, stores a value in one-
complement, what is the range of numbers that can be stored in this
register?
The value represented in one’s complement is (-2n-1+1; 2n-1-1), and in
MIPS we have 32 bits which means the range is (-231+1; 231-1)
2. What is the sequence of MIPS instructions that allow implementing the
following sentence in C language? (a and b are int variables)
a = b + c + 100;
li $t3,100
add $t0, $t1,$t2
add $t0, $t0,$t3
3. Consider the following fragment of C code:
for (i=0; i<=10; i=i+1)
a [i] = b[i] + c;
Assume that a and b are arrays of words and that the base address of a is in
$t0 and the base address of b is in $t1. Register $t2 is associated with
variable i and register $s0 with the value of c. You may also assume that any
address constants you need are available to be loaded from memory.
a. Write the code for MIPS.
.text
main:
li $t2, 0
la $t0, a
la $t1, b
loop:
beq $t2, 11, end
lw $t3, ($t1)
add $t3, $t3, c
sw $t3, ($t0)
add $t0, $t0, 4
add $t1, $t1, 4
add $t2, $t2, 1
j loop
end:
li $v0, 10
syscall
b. How many instructions are executed during the running of this code if
there are no array out-of-bounds exceptions thrown?
Answer: 82
c. How many memory data references will be made during execution?
Answer: 24
4. Write a program, using the MIPS 32 assembly language,
a. To calculate the sum of the first 100 numbers. The result must be
stored in register $v0.
.text
main:
li $a0,100
move $t0,$a0
li $t1,0
loop:beq $t0, 0, end
add $t1, $t1, $t0
sub $t0, $t0, 1
j loop
end: move $v0, $t1
b. Print the result.
Move $a0, $v0
li $v0, 1
syscall
5. Determine if the number stored in $t2 is even. If $t2 is even the program
stores 1 in $t1, else stores 0 in $t1
Print the result.
loop:
beq $t2, 1, end1
beq $t2, 2, end2
sub $t2, $t2, 2
j loop
end1: li $t1, 0
end2: li $t1, 1
move $a0, $t1
li $v0, 1
syscall
6. Write a program that reads two numbers. The program must print what
number is the largest.
.data
enter1: .asciiz “Enter first number: “
enter2: .asciiz “\nEnter second number: “
main:
li $v0, 4
la $a0 , enter1
syscall
li $v0, 5
syscall
move $t1, $v0
la $a0 , enter1
syscall
li $v0, 5
move $t2, $v0
bge $t1, $t2, end1
bge $t2, $t1, end2
end1:
li $v0, 1
move $a0, $t1
syscall
end2:
li $v0, 1
move $a0, $t2
syscall
7. Write a program to read a number. The program must indicate if the
number is odd or even.
.data
Ending1: .asciiz “odd”
Endind2: .asciiz “even”
.text
main:
loop:
beq $t2, 1, end1
beq $t2, 2, end2
sub $t2, $t2, 2
j loop
end1: li $v0, 4
la $a0, Ending1
end2: li $v0, 4
la $a0, Ending2
syscall
8. Write a program that reads two integer numbers A and B. The program
must indicate if one of these numbers is multiple of the other one.
.data
enter1: .asciiz “Enter first number: “
enter2: .asciiz “\nEnter second number: “
yes1: .asciiz “\nA is multiply of B”
yes2: .asciiz “\nB is multiply of A”
no1: .asciiz “\nA and B aren’t multiply of each other”
main:
li $v0, 4
la $a0 , enter1
syscall
li $v0, 5
syscall
move $t1, $v0
la $a0 , enter1
syscall
li $v0, 5
syscall
move $t2, $v0
bge $t1, $t2, end1
bge $t2, $t1, end2
end1: add $t4, $t2, 0
mul $t3, $t1, $t2
loop1:
beq $t2, $t1, good1
beq $t2, $t3, bad1
add $t2, $t2, $t4
j loop1
end2: add $t3, $t1, 0
mul $t4, $t1, $t2
loop2:
beq $t1, $t2, good2
beq $t1, $t4, bad1
add $t1, $t1, $t3
j loop2
good1:li $v0, 4
la $a0, yes1
syscall
good2:li $v0 , 4
la $a0, yes2
syscall
bad1: li $v0,4
la $a0, no1
syscall
li $v0, 10
syscall
9. Write a program to print on the screen number from 1-9.
.data
Space: .asciiz “ “
.text
main:
li $t0, 1
loop:
beq $t0, 10, end
add $a0, $t0, 0
li $v0, 1
syscall
li $v0, 4
la $a0, Space
add $t0, $t0, 1
j loop
end: li $v0, 10
syscall
10.Write a program, using the MIPS32 assembly language that reads a
number N and prints the following:
1
12
123
1234
…..
1 2 3 4 5 …. N
.data
Enter: .asciiz “enter number”
Space: .asciiz “\n”
.text
main:
li $v0, 4
la $a0, Enter
syscall
li $v0, 5
syscall
move $t0, $v0
sub $t0, $t0, 1
li $t2, 1
hmmm:
add $t2, $t2, 1
li $t1, 0
loop:
add $t1, 1
add $a0, $t1, 0
li $v0, 1
syscall
beq $t1, $t2, end
j loop
end:
li $v0, 4
la $a0, Space
bne $t2, $t0, hmmm
li $v0, 10
syscall
11.Write a function with three arguments. In register $a0 receives the init
address of a string, in register $a1 receives the ASCII code of a char, and
in register $a2 receives the ASCII code of other char. The function must
replace in the string any occurrence of the char stored in $a1 by the char
stored in register $a2.
.text
main:
la $a0, A \\ a is the string
li $a1, c \\ c is int of a character
loop:
lw $a2, ($a0)
beq $a2, $a1, hit
add $a0, $a0, 4
j loop
hit:
beq $a2, 0, end
add $a2, $a1, 0
add $a0, $a0, 4
j loop
end:
li $v0, 10
syscall
12.Consider the following program. Write an equivalent program, using the
MIPS 32 assembly language.
void function1 ()
{
int z;
int array[1024]; // local variable
for (i = 0; i < 1024; i++)
array[i] = i;
z = sum(array, 1024);
print_int(z);
}
int sum(int array[], int n)
{
int s = 0;
int i;
for (i = 0; i < n; i ++)
s = s + array[i];
return s;
}
.data
mem:.space 4096
.text
main:
la $t0, mem
li $t1, 0
loop:
beq $t1, 1024, end
sw $t1, ($t0)
add $t1, $t1, 1
add $t0, $t0, 4
j loop
end:
li $t2, 0
sub $t0, $t0, 4092
li $t3, 0
loop2:
beq $t3, 1024, bruh
lw $t1, ($t0)
add $t2, $t2, $t1
add $t0, $t0, 4
add $t3, $t3, 1
j loop2
bruh:
move $a0, $t2
li $v0, 1
syscall
13.Translate to assembly the following functions:
inf f (int k)
{
int v[100];
int i = 0;
int s = 0;
for (i = 0; i < k; i = i +2)
v[i] = g(k + 2);
for (i = 0; i < k; i = i + 2)
s = s + v[i];
return (s);
}
int g(int k)
{
if (k % 2 == 0)
return (k * k + 2);
else
return k * (-1);
}
.data
mem: .space 400
.text
main:
li $v0, 5
syscall
move $t4, $v0
add $s0, $t4, 0
add $t4, $t4, 2
la $t0, mem
li $t2, 0
li $t1, 0
loop:
jal function
sw $t4, ($t0)
add $t0, $t0, 8
add $t2, $t2, 2
bge $t2, $s0, peter
j loop
function:
loop2:
move $t3, $t4
beq $t3, 1, end1
beq $t3, 2, end2
sub $t3, $t3, 2
j loop2
end1:
mul $t4, $t4,-1
jr $ra
end2:
mul $t4, $t4,$t4
add $t4, $t4, 2
jr $ra
peter:
li $t2, 0
li $t3, 0
sub $t0, $t0, 392
loop3:
lw $t4, ($t0)
add $t2, $t2, $t4
add $t0, $t0, 8
add $t3, $t3, 2
bge $t3, $s0, bigchungus
j loop3
bigchungus:
li $v0, 1
move $a0, $t2
syscall
li $v0, 10
syscall
14.Let be a 16 bit computer, with byte addressing and a set of 60 machine
instructions. The computer has 8 registers. Show the instruction format
for the following hypothetical instruction ADDV R1, R2, M, where R1
and R2 are registers, and M is memory address.
8 registers 3 bits for register.
60 instructions 6 bits for instruction
Bits for memory address = 16 – 6 – 2 * 3 = 4 bits
Format
opcode(6 Resgister(3 bits) Resgister(3 bits) memory address( 4 bits)
bits)
15.A 32 bit computer with byte addressing, has 64 machine instructions, and
128 registers. Given the instruction SWAPM addr1, addr2 that swaps the
content of the memory address addr1 and addr2.
a) What is the memory space addressable by this computer?
Answer: 232 bytes
b) What is the instruction format?
64 instructions 6 bits to address function
128 registers 7 bits for register
opcode(6 bits) Resgister1(7 Resgister2(7 12 bits
bits) bits)
c) Write a program fragment, using the MIPS 32 assembly language,
equivalent to the previous instruction.
.text
swap:
lw $t4, ($t2)
lw $t3, ($t1)
sw $t4, ($t1)
sw $t3, ($t2)
d) If the above instruction must occupy one word, what is the range of
addresses that can be addressed by addr1 and addr2?
Answer: 0 232
16.A 32-bit computer with memory addressed by bytes has 64 registers. We
want to design the format for the instructions of this computer, assuming:
- The computer has 115 instructions.
- The execution model in register-register.
- The computer has the following types of instructions:
· Instructions for transferring data between memory and registers. These
instructions use two types of addressing modes: direct, and base-register
addressing
· Arithmetic and logic instructions.
· Branch instructions. There are two types of instructions: unconditional
to a memory address, and conditional branches. For conditional branches,
the condition is expressed in a register, and the branch is indicated with
PC-relative addressing.
a) Design the format for the instructions of this computer, having into
account that all instructions occupy one word.
115 instructions 7 bits for instruction
64 registers 6 bits for register
Tranfering data direct
opcode(7 bits) resgister1(6 bits) address (19 bits)
Based register
Opcode resgister1(6 resgister2(6 bits) displacement( 13 bits)
(7bits) bits)
Arithmatic and logic
Opcod resister1 resgister2 resgister Offset
e (6 bits) (6bits) 3 (7 bits)
(7 bits) (6bits)
Unconditioned branch
Opcode(7 addresss (25 bits)
bits)
Conditioned
Opcode (7 resgister( 6bits) address (20 bits)
bits)
b) If the displacement used in the instructions with base-register
addressing uses two-complement, what is the maximum value for
these displacements?
Answer: 212 - 1
c) If the address used in the unconditional branch instructions use binary
code (unsigned), what is the range of addresses for the branch?
Answer: 0 225 - 1
d) What is the model of this computer, RISC or CISC?
Answer: RISC