0% found this document useful (0 votes)
48 views

Exercises 06 - MIPS - 2020

The document discusses MIPS assembly language instructions for conditional and unconditional branching, input/output system calls, and segment directives used to define data and program sections. It provides examples of instructions for comparing values, incrementing variables in loops, and counting elements in an array that are divisible by a number.

Uploaded by

Era
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

Exercises 06 - MIPS - 2020

The document discusses MIPS assembly language instructions for conditional and unconditional branching, input/output system calls, and segment directives used to define data and program sections. It provides examples of instructions for comparing values, incrementing variables in loops, and counting elements in an array that are divisible by a number.

Uploaded by

Era
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

FACULTY OF COMPUTER SCIENCE AND

ENGINEERING

FACULTY OF COMPUTER SCIENCE AND ENGINEERING

Assembler MIPS

Computer Architecture
Exercises 6
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Program counter
l An assembler program is a sequence of instructions that must be
executed by the processor
l The instructions (the program) are located in the memory and are
executed in order, where $pc (program counter) register points to
the address of the next instruction that has to be executed.

2
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Instructions for conditional branching


Instruction Syntax Meaning
Branch on equal beq $1,$2,CONST if ($1 == $2) go to
PC+4*CONST
Branch on not equal bne $1,$2,CONST if ($1 != $2) go to
PC+4*CONST
Set on less than slt $1,$2,$3 $1 = ($2 < $3)
Set on less than slti $1,$2,CONST $1 = ($2 < CONST)
immediate
Branch greater than bgt $1,$2,CONST if ($1 > $2) go to
PC+4*CONST
Branch less than blt $1,$2,CONST if ($1 < $2) go to
PC+4*CONST
Branch greater than or bge $1,$2,CONST if ($1 >= $2) go to
equal PC+4*CONST
branch less than or ble $1,$2,CONST if ($1 <= $2) go to
equal PC+4*CONST
3
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Instructions for unconditional branching

Operation Syntax Meaning


Jump j CONST goto address CONST
Jump register jr $1 goto address $1
Jump and link jal CONST $31 = PC + 4; goto CONST

4
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Problem 9
§ Write an assembly program which will compare the values of the both
variables stored in $s0 and $s1 and will write the greater value in register
$s2. if ($s0 < $s1 ) {$s2 = $s1;} else {$s2 =$s0;}

slt $t0, $s0, $s1 #if $s0 <$s1 , then $t0ß1, else $t0ß0
beq $t0, $zero, else #if $t0==0, then $s0 >=$s1 and as next instruction
the else instruction is taken, on contrary, the next
instruction is taken
add $s2, $s1, $zero #if the condition in beq is not fulfilled, this
instruction is executed which to $s2 is assigned
the value of $s1 (because $s0 >=$s1)
j end #unconditional jump. The program directly
jumps to the instruction on mark end. The next
instruction cant be executed since it is
executed if $s0 >=$s1
else: add $s2, $s0, $zero #we arrive here if $s0 >=$s1, therefore in
$s2 is stored the value of $s0
5
end:
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Problem 10
§ Write an assembly program for the expression:
while (i < 20) {
A[i]=A[i+2] +100
i+=1}
under assumption that the variable i is stored in register $s3, and the start
address of the field A is stored in register $s6.
loop: slti $t0, $s3, 20 # if $s3 <20, then $t0ß1, else $t0ß0
beq $t0, $zero, end # if the above condiution is not fullfiled, go to end
sll $t1, $s3, 2 # $t1ß4i
add $t1, $t1, $s6 # $t1ß&A[i]
lw $t2, 8($t1) # $t2ßA[i+2]
addi $t2, $t2,100 # $t2ßA[i+2]+100
sw $t2, 0($t1) # A[i]ßA[i+2]+100
addi $s3, $s3, 1 # $s3ßi+1
j loop #unconditional jump
end : 6
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Problem 11
§ Write an assembly program which on address stored in register $s5,
will write the number of elements of field A which are divisible with
3. The start address of the field is stored in register $s0, and the
number of elements which are contained in the field is stored in
register $s1.

7
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

# s1 number of elements
add $t1, $zero, $zero # t1=0 – counter of el. divisible with 3
add $t2, $zero, $zero # t2=0 – counter of el. of the field
addi $t3, $zero, 3 # t3=3 – necessary for checking divisibility by 3
loop: beq t2, s1, write # if it is end of array el. then exit the loop
sll t4, t2, 2 # $t4=$t2*4
add $t4, $s0, $t4 # $t4=&A[i]
lw $t5, 0($t4) # $t5=A[i]
div $t5, $t3 # divide A[i] and 3
mfhi $t5 # store the remainder in t5
bneq $t5, $zero, next # if remainder is not equal to 0, go to next element
addi $t1, $t1, 1 # if the remainder is 0, increment the counter t1
next: addi $t2, $t2, 1 # increment counter t2 by 1
j loop # jump to loop – check next element
write: sw $t1, 0($s5) # store the result on address given in $s5

8
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Segment directives
§ In order to define the data (variables) part in the program the
directive .data is used
§ The data defined after this directive are placed in the data segment
§ In order to define the data we use the names and the data directives:
§ Name_of_variable: .directive value
§ Data directives
Directive Meaning
.ascii str Assemble the given string in memory. Do not null terminate
.asciiz str Assemble the given string in memory. Do null terminate
.byte B1,…,BN Assemble the given bytes (8-bit integers)
.half H1,…,HN Assemble the given halfwords (16-bit integers)
.word W1…,WN Assemble the given words (32-bit integers)
.space n Allocate n bytes of space in the data segment

§ The instructions of the program are defined after the directive .text
§ The items defined after this directive are placed into the text segment

9
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

System calls for input and output


§ The instruction syscall is used for generating system calls depending
on the content in $v0 and the arguments given in $a0-$a3. If the
syscall should return a result, it is placed in the $v0 registry.

Name Code Input Output


print_int $v0 = 1 $a0 = integer to print prints $a0 to standard
output
print_string $v0 = 4 $a0 = address of first Prints string with start
character address in $a0
read_int $v0 = 5 integer read from standard
input placed in $v0
read_string $v0 = 8 $a0 = address to place reads standard input into
string, address in $a0
$a1 = max string length
Exit $v0 = 10 program exits

10
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Problem 13
Write an assembly program that will read a number from standard input and
will check whether that number exists in the array of elements defined in the
data segment. The length of the array is stored in a variable with name size in
the data segment. You need to print “Exists” to standard output if the number
exists, or “Not exists” if it doesn’t.

.data
numbers: .word 1, 2, 3, 5, 6, 7, 19 # the array of numbers
size: .word 7 # length of the array
msg1: .asciiz “Enter a number.” # array of chars, null terminated
msg2: .asciiz “Exists” # array of chars, null terminated
msg3: .asciiz “Not exists” # array of chars, null terminated
.text
la $s0, numbers # load address into $s0
la $s1, size # load size into $s1
lw $s2, 0($s1) # number of elements in the array

11
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Problem 13
la $a0, poraka1 # load address of msg1 into $a0
li $v0, 4 # used for printing
syscall
li $v0, 5 #used for reading a number
syscall
add $t0, $0, $v0 # load read value into $t0
addi $s3, $zero, 0 # next element of the array
loop: beq $s3, $s1, endLoop
sll $t1, $s3, 2 # multiply with 4
add $t1, $t1, $s0 # add array base address
lw $t2, 0($t1) # read a[i] and store into $t2
beq $t2, $t0, found #number is found
addi $s3, $s3, 1
j loop
endLoop: la $a0, poraka3 # load address of msg3 into $a0
j print #jump to end, to do syscall
found: la $a0, poraka2 # load address of msg2 into $a0
print: li $v0, 4 # put 4 into $v0 for printing
12
syscall # print message in $a0
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Procedure call
• The input parameters are placed in $a0, $a1, $a2 и $a3
• The jal lokacija_na_procedura instruction is called. jal (jump and link) saves the
address of the next instruction of the main program ($pc+4) in the $ra registry,
and as a value of $pc the location of the procedure is set.
• Data space is provided, required to perform the procedure
• The procedure is executed
• The results are placed in the $v0 and $v1 registers
• The jr $ra is called. jr places the content of $ra in $pc and returns to the program.

13
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Problem 14
§ Write an assembler program that will print to standard output how
many numbers from the array numbers are prime. The size of the
array is stored in a variable with name size. Use procedures to solve
the exercises.

.data
numbers: .word 1, 2, 3, 5, 6, 7, 19
size: .word 7
.text
la $s0, numbers # load array address into $s0
la $s1, size # load size into $s1
addi $s2, $zero, 0 # prime number counter
addi $s3, $zero, 0 # counter for passed numbers
lw $s4, 0($s1) # index counter

14
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Problem 14
start: beq $s4, $s3, End
sll $t0, $s3, 2 # multiply with 4, $t0=$s3*4
add $t0, $t0, $s0
lw $a0, 0($t0) # read numbers[i] and set as first argument to the
procedure
jal Prime # call Prime procedure
add $s2, $s2, $v0 # Add the returned value to the prime number counter
addi $s3, $s3, 1 # increment index counter
j start
End:
add $a0, $zero, $s2 # put Prime counter into $a0
li $v0, 1 # put1 into $v0
syscall # print value of $a0

15
FACULTY OF COMPUTER SCIENCE AND
ENGINEERING

Problem 14
Prime:
addi $v0, $zero, 1 # (1 if exists, 0 otherwise)
addi $t0, $zero, 1
beq $t0, $a0, Exit # if number == 1, it’s prime, exit
li $t0, 2
beq $t0, $a0, Exit # if number == 2, it’s prime, exit
srl $t2, $a0, 1 # divide by 2 and add 1
addi $t2, $t2, 1
Next:
beq $t0, $t2, Exit # iterate through n/2+1
div $a0, $t0 # $a0 / $t0
mfhi $t1 # put the remainder into $t1
beq $t1, $zero, NotPrime # if remainder == 0, number is not prime
addi $t0, $t0, 1 # otherwise, increment quotient
j Next
NotPrime: li $v0, 0 # return 0
Exit : jr $ra # return to the calling program
16

You might also like