SCC150 – MIPS/Assembly Week 14/16 Assessed Practical
SCC – Lancaster University Week 14/16
Copyright By PowCoder代写 加微信 powcoder
Assessed Exercise
• This exercise is assessed
• You should work alone
• The deadline is Friday Week 16, 4pm GMT
• You must submit your work before the deadline
• You should submit
• ASM file containing your code
• Text file (doc, pdf, txt, odt) containing your predicted marks
NOTE: Teams labs spaces are not moderated. Please be careful when sharing information about your code. All submissions are processed by a plagiarism checking tool and code similarities can get you in trouble.
Be careful
• Do not share code or code solution in teams chat or the forum. • If you need help, talk to one of the TAs one-to-one.
• All code submissions are checked by plagiarism checking tools.
• If you have any questions about coursework please ask academics. • Do not trust information that you read on group chat.
• Exploit lab time to discuss any problem with Tas and academics.
• If you are not sure, ask us.
• If you are facing any difficulties and you cannot meet deadlines, please contact out teaching office as soon as possible.
• Teaching academics cannot grant extensions.
Coursework context
• Build a 2d drawing application using MARS. • Support a simple cli interface.
• Supported operations:
• cls: paint all pixels to a specific colour
• Draw row: paint white all pixels of a horizontal line. • Draw column: paint white all pixels of a vertical line. • Draw a triangle in a different colour
• Your solution should follow the coursework requirement to get full marks (e.g. use procedure where asked, follow procedure rules).
• Using syscall to print to screen • Using syscall to read an integer • Interpret instruction
• Drawing on screen
• Implementing procedure • Triangles
• Mark yourself
Syscall – Printing a String
#using .data to store the string you want to print
#.asciiz tells it that it has an ascii string and that it ends with a zero #info is just a label
info: .asciiz “This string has instructions”
#back to main program here (.text)
addi $v0,$zero,4 la $a0, info
#tell syscall to print string (put code 4 into $v0) #load the string address (label info) into $a0 #$a0 is the parameter for syscall
#call syscall – it will only print now
Syscall Service Numbers
System call code
$a0=integer
print_float
$f12=float
print_double
$f12=double
print_string
$a0=string
integer ($v0)
read_float
float ($f0)
read_double
double ($f0)
read_string
$a0=buffer, $a1=length
$a0=amount
Syscall – Reading an Integer
• To read an integer, you must put code 5 into syscall ($v0)
• When you call syscall, you can type the integer in using the bottom window
• The result is stored in $v0 here
Conditional Branch
• Different conditions can be tested
• branch on equal: beq
• branch on not equal: bne
• set on less than: slt (used to support beq and bne)
• PC-relative addressing
• take the address offset and sign extend to 32 bits • multiply by four (shift left by two places)
• add to the program counter
• 16-bit PC-relative
• 2^16=65536words
• -32768 or +32767 instr. away from PC
Branch Example For loop
for (i = 0; i < 10; i++) j++;
addi $s0, $zero, 0 loop: bne $s0,10, Exit
addi $s1,$s1,1 addi $s0, $s0, 1 j loop
register mapping
i: $s0 j: $s1
Interpret Instruction
• You’ve told the user what to do
• You’ve read the integer
• Decide what to do with the instruction • There are two types of instruction:
• Select task
1. cls (colour background) 2. Row
4. Triangles (A only)
• Select colour – there should be several choices of colour
Drawing on screen
• You must be able to carry out the instruction
• Week 12’s practical instructions will help with this if you’ve forgotten
• Use heap for display now because print string will use the data section • CLS
• Fill in the background of the screen in the correct colour • Row
• Draw a horizontal line (in the correct colour) • Column
• Draw a vertical line (in the correct colour) • Triangle
• Draw a filled triangle starting at a peak of given coordinates (in the correct colour) • Exit
CLS – Example (in blue)
lui $s0,0x1004
addi $t8,$zero,0x00ff addi $t0,$s0,0
lui $s1,0x100C
drawPixel:
sw $t8,0($t0)
addi $t0,$t0,4
bne $t0,$s1,drawPixel
#bitmap display base address in $s0 (heap) #set colour to blue in $t8
#initialise $t0 to base address, will count #end of screen area in $s1
#store colour $t8 in current target address #increment $t0 by one word
#if haven’t reached the target yet, repeat
Drawing lines - algorithm
1. Find start point and paint pixel.
2. Add the appropriate number to find the memory location of the next pixel and paint.
3. If we have not reached the end of the line, go to 1.
Memory layout
• Each pixel is a word; 0x00000000
• Pixel orientation: left to right, top to bottom. • (0, 0)0x10040000
• (1, 0)0x10040000 + 1*4
• (2, 0)0x10040000 + 2*4
• (0,1)0x10004000 + 0*4 + 1*265*4 • (1, 2)0x10004000 + 1*4 + 2*265*4
NOTE: MIPS mult instruction requires access to a co-processor. We suggest the use of loops and additions to multiply.
Accessing Memory
lw $s1,100($s2)
“load word” destination register
sw $s1,100($s2)
source address of word
lw,sw are I-type Instructions
destination address source register
“store word”
Example – cls, row
Example - column
Steps for procedure implementation
1. Placeparametersinaplacewheretheprocedurehasaccess.
2. Transfercontroltotheprocedure.
3. Acquirethestorageresources(e.g.variables,arrays)needed for the procedure.
4. Performthedesiredtask.
5. Placetheresultvalueinaplacewherethecallercanaccessit.
6. Returncontroltothepointoforigin.
CLS – Example (in blue)
lui $s0,0x1004
addi $t8,$zero,0x00ff addi $t0,$s0,0
lui $s1,0x100C
drawPixel:
sw $t8,0($t0)
#bitmap display base address in $s0 #set colour to blue in $t8
#initialise $t0 to base address, will count #end of screen area in $s1
#store colour $t8 in current target address #increment $t0 by one word
addi $t0,$t0,4
bne $t0,$s1,drawPixel#if haven’t reached the target yet, repeat
MIPS registers for procedure calls
“argument” registers in which to pass parameters
$v0 and $v1:
return value registers
return address register
MIPS instructions for procedure calls
jal ProcedureAddress
“jumpandlink”
label to jump to
• jal stores the address of the next instruction in $ra
• ...and then jumps to ProcedureAddress
•toget back,weuse“jumpregister” jr $ra
“jumpregister”
Preserving registers – register spilling
• Sometimes a procedure needs to use more registers than just four arguments and two return values.
• Register content must be preserved during procedure call
• Moving the contents of registers to main memory is called
spilling registers.
• Registers are stored to memory using a conceptual data
structure known as a stack.
• The stack pointer register $sp points to the contents of the register most recently pushed onto the stack.
Preserving registers – which registers?
P&H fig. 2.15
• MIPS convention
• If usage of these registers is avoided no spilling of registers on the stack is required.
• s registers must be restored after procedure call
• To get the triangles working, you need a good understanding of the bitmap memory structure – how to move between rows and columns reliably
• Set start row and column.
• In the first row, draw a single pixel
• Go to the next row, draw extra pixels on either side of the single pixel in the row above (the number of extra pixels is determined by “growth”).
• Length of row = length of previous row + 2*growth
• Don’t worry about the triangle wrapping around
0 000 00000
Example - triangles
Input data:
1 (cls), 3 (yellow)
4 (triangle), 5 (blue), 4 (row), 55 (col), 2 (growth) 4 (triangle), 1 (red), 200 (row), 88 (col), 5 (growth)
• There will be an automated marking system that will test your code
• This will detect: • Procedures
• Use of syscalls
• In week 18, the Senior TAs will mark your work. You MUST attend this
session. The TA will:
• Provide feedback
• Clarify any queries about the automated marking • Ask questions about your code
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com