程序代写 COMP2421 Computer Organization 2019/20 Semester 1

COMP2421 Computer Organization 2019/20 Semester 1
Lab 4 MIPS Assembly Language Basics II
In this lab, the following concepts of MIPS assembly language will be discussed. ➢ Instruction types
➢ Conditional expressions and Branching

Copyright By PowCoder代写 加微信 powcoder

➢ Bitwise Operations
MIPS instructions fall into three groups: R-type, I-type, and J-type. R-type Instructions (R for register)
31 2625 2120 1615 1110 65 0
opcode rs rt rd
Example: add $rd, $rs, $rt
[opcode] Operation code
[rs] First source register
[rt] Second source register
[rd] Destination register
[shamt] Amount of bits to shift (used in shift instructions)
[funct] Control codes that differentiate the different instructions with the same opcode
Format of I-type Instructions (I for immediate)
31 2625 2120 1615 0
opcode rs rt
Example: addi $rt, $rs, imm

COMP2421 Computer Organization
2019/20 Semester 1
[opcode] Operation code
[rs] Source register / base
[rt] Target register
[imm] Offset / Immediate value
Format of J-type Instructions (J for jump)
31 2625 opcode
Example: j target
[opcode] Operation code (for example: j, opcode = 2). [target] Address of Target.
Read the following program:
# Program: sum of an array
.word 17, 2, 3, -5, 4
# declare an array
.globl main
li $s1, li $t1, li $t2,
beq $t2, lw $v1, add $s1, addi $t1, addi $t2, j for
0#zerothesum 0#initindexto0
0 # init loop counter
array($t1)
# for(i = 0; i < 5 ; i++) # sum = sum + array[i] # index++ # counter++ [Question: Can you identify the type of each instruction?] COMP2421 Computer Organization 2019/20 Semester 1 For the statement, “beq $t2, 5, endfor”, it is a conditional jump. It “jumps” or “branches” to the label, “endfor” if $t2 is equal to 5. The statement, “j for” is an unconditional jump. It “jumps” to the label “for” once it is executed. This is how we implement “looping” in assembly language. Bitwise Operations Logical Operations R-type logical bitwise operators: and, or, nor, xor and $rd, $rs, $rt or $rd, $rs, $rt nor $rd, $rs, $rt xor $rd, $rs, $rt I-type logical bitwise operators: andi, ori, xori andi $rt, $rs, imm ori $rt, $rs, imm xori $rt, $rs, imm An example of using bitwise operation – If we want to extract the last three bits value from a register, we may apply the “and” operation to extract the required bits’ value. See below program fragment for how the last three bits can be extracted and stored to $s0. li $t0, 0xFD and $s0, $t0, $t1 # 11111101 # 00000111 # 00000101 [Question: How can the 4th to 7th bits of $t0 be extracted and stored in $s1?] -3- COMP2421 Computer Organization 2019/20 Semester 1 Shift Operations srl sll sra srl sll sra [shift right logical] [shift left logical] [shift right arithmetic] $t1, $t2, imm $t1, $t2, imm $t1, $t2, imm [Question] Suppose we have the following 32-bit values: $t1: 0101 1100 0101 1100 0101 1100 0101 1100 $t2: 1001 1000 1001 1000 1001 1000 1001 1000 What are the results of the following instructions? a) sll $t0, $t1, 2 b) sll $t0, $t1, 3 c) srl $t0, $t2, 3 d) sra $t0, $t2, 4 Rotate Operations (Circular Shift) rol [rotate left] ror [rotate right] rol $t1, $t2, imm ror $t1, $t2, imm [Question] Suppose we have the following 32-bit values: $t1: 1101 0110 1101 0110 1101 0110 1101 0110 $t2: 0011 1100 0011 1100 0011 1100 0011 1100 What are the results of the following instructions? a) rol $t0, $t1, 1 b) rol $t0, $t1, 3 c) ror $t0, $t2, 1 d) ror $t0, $t2, 3 COMP2421 Computer Organization 2019/20 Semester 1 [Exercise] Modify the “sum of an array” program and use the following array values: array: .word 15, 23, -13, -75, 44 Sum the positive integers to “Positive Sum” and negative integers to “Negative Sum”. 程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com