Sie sind auf Seite 1von 11

A - Official Use Only

Monash University
Semester Two Mid Semester Test 2016

Faculty of Information Technology

EXAM CODES: FIT1008


TITLE OF PAPER: INTRODUCTION TO COMPUTER SCIENCE
TEST DURATION: 35 minutes
READING TIME: 5 minutes

THIS PAPER IS FOR STUDENTS STUDYING AT:


o Berwick ü Clayton ü Malaysia o Off Campus Learning o Open Learning
o Caulfield o Gippsland o Peninsula o Enhancement Studies o Sth Africa
o Pharmacy o Other (specify)

During an exam, you must not have in your possession, a book, notes, paper, electronic device/s, calculator,
pencil case, mobile phone or other material/item which has not been authorised for the exam or specifically
permitted as noted below. Any material or item on your desk, chair or person will be deemed to be in your
possession. You are reminded that possession of unauthorised materials in an exam is a discipline offence under
Monash Statute 4.1.

No examination papers are to be removed from the room.

AUTHORISED MATERIALS

CALCULATORS o YES ü NO
OPEN BOOK o YES ü NO
SPECIFICALLY PERMITTED ITEMS o YES ü NO

STUDENT ID __ __ __ __ __ __ __ __

Page Mark
3
5
7
9
Total

Page 1 of 9
This page intentionally left blank, use if needed but it will not be
marked.

Page 2 of 9
Question 1 [3 marks]
This question is about MIPS programming. Translate the following Python code faith-
fully into MIPS assembly language. Note that in this piece of code all variables are
global variables. Add comments to your MIPS code to facilitate readability. Use only
instructions or pseudo-instructions in the MIPS reference sheet.
a = 3
b = 5
c = 0

while b > a :
if b % 2 == 0:
c = c + b
else :
c = c -b
b = b -1

Page 3 of 9

3
This page intentionally left blank, use if needed but it will not be
marked.

Page 4 of 9
Question 2 [2 marks]
For each function below, provide best and worst case complexity in Big O notation.
Explain your answer – no explanation means no marks.

1. def function_a ( a_list ):


n = len ( a_list )
for i in range (n -1 , -1 , -1):
for j in range (10):
a_list [ i ] = j + i - 1

2. def function ( a_list ):


n = len ( a_list )
for j in range ( n ):
i = j
while a_list [ i ] < 0:
a_list [ i ] = j + i - 1
i = i - 1
a_list [ j ] = i

Page 5 of 9

2
This page intentionally left blank, use if needed but it will not be
marked.

Page 6 of 9
Question 3 [3 marks]
A quick-sort implementation is given below:
def quick_sort ( a_list ):
start = 0
end = len ( a_list ) -1
quick_sort_aux ( a_list , start , end )

def quick_sort_aux ( a_list , start , end ):


if start < end :
boundary = partition ( a_list , start , end )
quick_sort_aux ( a_list , start , boundary -1)
quick_sort_aux ( a_list , boundary +1 , end )

• Write a method partition, that completes this implementation. Your partition


algorithm should have linear time complexity.

• Is quick-sort stable? Explain your answer. No explanation means no marks.

Page 7 of 9

3
This page intentionally left blank, use if needed but it will not be
marked.

Page 8 of 9
Question 4 [2 marks]
Consider the following python code, which you wish to translate into MIPS.
a = 2
b = 3

def my_function (a , b ):
i = a
# HERE
while i < b :
a = a + b
i +=1
return a

my_function (a , b )

Draw the memory diagram of the stack segment when code execution reaches the line
marked with the comment HERE. The diagram should show, the stack pointer and frame
pointer and any information that may be stored as part of the function calling convention
discussed in the lectures. Global variables are not on the stack, so they are not to be
shown. Before reaching my_function, ra = 0x00400000 and fp = 0x7FFFB3114.

Page 9 of 9

3
FIT1008 Supplementary Material - MIPS Reference Sheet

Table 1: SPIM system calls Table 3: Assembler directives


Call Service Arguments Returns Notes .data assemble into data segment
code .text assemble into text (code) segment
($v0) .byte b1[, b2, ...] allocate byte(s), with initial value(s)
1 Print integer $a0 = value to - value is signed .half h1[, h2, ...] allocate halfword(s), with initial value(s)
print .word w1[, w2, ...] allocate word(s) with initial value(s)
4 Print string $a0 = address - string must .space n allocate n bytes of uninitialized, unaligned space
of string to be terminated .align n align the next item to a 2n -byte boundary
print with ’\0’ .ascii ”string” allocate ASCII string, do not terminate
5 Input integer - $v0 = entered value is signed .asciiz ”string” allocate ASCII string, terminate with ’\0’
integer
8 Input string $a0 = address – returns if
Table 4: Function calling convention
to store string $a1-1 charac-
at ters or Enter On function call:
Caller: Callee:
$a1 = max- typed, the
saves temporary registers on stack saves $ra and $fp on stack
imum number string is ter-
passes arguments on stack copies $sp to $fp
of chars minated with
calls function using jal fn label allocates local variables on stack
’\0’
9 Allocate $a0 = number $v0 = address - On function return:
memory of bytes of first byte Callee: Caller:
10 Exit - - ends simula- sets $v0 to return value clears arguments o↵ stack
tion clears local variables o↵ stack restores temporary registers o↵ stack
restores saved $fp and $ra o↵ stack uses return value in $v0
Table 2: General-purpose registers
returns to caller with jr $ra
Number Name Purpose
R00 $zero provides constant zero
R01 $at reserved for assembler Table 5: Instruction Set
R02, R03 $v0, $v1 system call code, return value A partial instruction set is on the next page. The following conventions apply.
R04–R07 $a0--$a3 system call and function arguments Instruction Format
R08–R15 $t0--$t7 temporary storage (caller-saved) Rsrc, Rsrc1, Rsrc2: source operand(s), - must be a register value(s)
R16–R23 $s0--$s7 temporary storage (callee-saved) Src2; source operand - may be an immediate value or a register value
R24, R25 $t8, $t9 temporary storage (caller-saved) Rdest: destination, must be a register
R26, R27 $k0, $k1 reserved for kernel code Imm: Immediate value, may be 32 or 16 bits
R28 $gp pointer to global area Imm16: Immediate 16-bit value
R29 $sp stack pointer Addr: Address in the form: o↵set(Rsrc) ie. absolute address = Rsrc + o↵set
R30 $fp frame pointer label: label of an instruction
R31 $ra return address ?: pseudoinstruction
Immediate Form -: no immediate form, or this is the immediate form
?: immediate form synthesized as pseduoinstruction
Unsigned form (append ’u’ to instruction name):
- : no unsigned form, or this is the unsigned form
Page 1
Table 6: MIPS instruction set
Instruction format Meaning Operation Immediate Unsigned form(u)
form
add Rdest, Rsrc1, Rsrc2 Add Rdest = Rsrc1 + Rsrc2 addi no overflow trap
sub Rdest, Rsrc1, Rsrc2 Subtract Rdest = Rsrc1 - Rsrc2 ? no overflow trap
mul Rdest, Rsrc1, Rsrc2 ? Multiply Rdest = Rsrc1 * Rsrc2 ? unsigned operands
mulo Rdest, Rsrc1, Rsrc2 ? Multiply Rdest = Rsrc1 * Rsrc2 ? unsigned operands
(with 32-bit overflow)
mult Rsrc1, Rsrc2 Multiply Hi:Lo = Rsrc1 * Rsrc2 - unsigned operands
(machine instruction)
div Rdest, Rsrc1, Rsrc2 ? Divide Rdest=Rsrc1/Rsrc2 ? unsigned operands
div Rsrc1, Rsrc2 Divide Lo = Rsrc1/Rsrc2; - unsigned operands
(machine instruction) Hi = Rsrc1 % Rsrc2
rem Rdest, Rsrc1, Rsrc2 ? Remainder Rdest = Rsrc1 % Rsrc2 ? unsigned operands
neg Rdest, Rsrc ? Negate Rdest = -Rsrc1 - no overflow trap
and Rdest, Rsrc1, Rsrc2 Bitwise AND Rdest = Rsrc1 & Rsrc2 andi -
or Rdest, Rsrc1, Rsrc2 Bitwise OR Rdest = Rsrc1 | Rsrc2 ori -
xor Rdest, Rsrc1, Rsrc2 Bitwise XOR Rdest = Rsrc1 ^ Rsrc2 xori -
nor Rdest, Rsrc1, Rsrc2 Bitwise NOR Rdest = ⇠(Rsrc1 | Rsrc2) ? -
not Rdest, Rsrc ? Bitwise NOT Rdest = ⇠(Rsrc) -
sll Rdest, Rsrc1, Rsrc2 Shift Left Logical Rdest = Rsrc1 << Rsrc2 - -
srl Rdest, Rsrc1, Rsrc2 Shift Right Logical Rdest = Rsrc1 >> Rsrc2 - -
(MSB=0)
sra Rdest, Rsrc1, Rsrc2 Shift Right Arithmetic Rdest = Rsrc1 >> Rsrc2 - -
(MSB preserved)
move Rdest, Rsrc ? Move Rdest=Rsrc - -
mfhi Rdest Move from Hi Rdest = Hi - -
mflo Rdest Move from Lo Rdest = Lo - -
li Rdest, Imm ? Load immediate Rdest=Imm - -
lui Rdest, Imm16 Load upper immediate Rdest=Imm16 << Imm - -
la Rdest, Addr(or label) ? Load Address Rdest=Addr - -
(or Rdest=label)
lb Rdest, Addr (or label ?) Load byte Rdest = mem8[Addr] - zero-extends data
lh Rdest, Addr (or label ?) Load halfword Rdest = mem16[Addr] - zero-extends data
lw Rdest, Addr (or label ?) Load word Rdest = mem32[Addr] - -
sb Rsrc2, Addr (or label ?) Store byte mem8[Addr] = Rsrc2 - -
sh Rsrc2, Addr (or label ?) Store halfword mem16[Addr] = Rsrc2 - -
sw Rsrc2, Addr (or label ?) Store word mem32[Addr] = Rsrc2 - -
beq Rsrc1, Rsrc2, label Branch if equal if (Rsrc1 == Rsrc2) ? -
PC = label
bne Rsrc1, Rsrc2, label Branch if not equal if (Rsrc1 != Rsrc2) ? -
PC = label
blt Rsrc1, Rsrc2, label ? Branch if less than if (Rsrc1 < Rsrc2) ? unsigned operands
PC = label
ble Rsrc1, Rsrc2, label ? Branch if less than or equal if (Rsrc1 <= Rsrc2) ? unsigned operands
PC = label
bgt Rsrc1, Rsrc2, label ? Branch if greater than if (Rsrc1 > Rsrc2) ? unsigned operands
PC = label
bge Rsrc1, Rsrc2, label ? Branch if greater than or if (Rsrc1 >= Rsrc2) ? unsigned operands
equal PC = label
slt Rdest, Rsrc1, Rsrc2 Set if less than if (Rsrc1 < Rsrc2) slti unsigned operands
Rdest=1
else Rdest=0
j label Jump PC = label - -
jal label Jump and link $ra = PC + 4; - -
PC = label
jr Rsrc Jump register PC = Rsrc - -
jalr Rsrc Jump and link register $ra = PC + 4; - -
PC = Rsrc
syscall System call depends on call code in $v0 - -

Page 2

Das könnte Ihnen auch gefallen