Docsity

MIPS Assembly and PCSPIM: Logic Operations and Programming - Prof. Thomas E. Murphy, Study notes of Engineering

Toggle sidebar
bg1
ENGR 2030 MIPS Assembly, PCSPIM
What we will cover
PCSPIM
Logic Operations
MIPS Assembly programming
Goals
Become familiar with MIPS assembly language programming and PCSPIM.
PCSPIM
PCSPIM is a MIPS assembler-simulator. The word size for the MIPS hardware is 32 bits (4-bytes).
PCSPIM MIPS assembly programs must end with a system call to terminate the program and a blank line
should be left at the end of the program (this is necessary due to a bug in some versions of PCSPIM).
li $v0,10 # system call to stop simulator
syscall
Pseudo Instructions
Our MIPS assembler-simulator supports pseudo instructions as well as native instructions. The MIPS
hardware we are working with supports only two conditional branches: beq and bne. However, our MIPS
assembler-simulator also supports the pseudo instructions: bgt, blt, bge, and ble. These pseudo instruction
conditional branches are translated into native instructions by the assembler. MIPS can simulate different
addressing modes similarly.
The pseudo instruction conditional branches are converted to a slt (set if less than) followed by either a beq
or bne, for example
ble $t8, $t9, label # branch if R8 <= R9
is translated into the native instructions
slt $t5, $t9, $t8 # set R5 if R9 < R8
beq $t5, $zero, label # branch if R5 = 0
Simulating Programs in PCSPIM
The PCSPIM simulator expects the data segment to start at 0x10010000 and the text or program segment to
start at 0x00400000.
Before assembling and simulating a program, make sure the MIPS settings allow pseudo instructions and
do not use the trap file (exceptions). Under Simulator Settings, make sure the box to Allow
pseudo instructions is checked and the box for Load trap (exception) file is not, see
Figure 3a.
Once a program is loaded (File – Open) into PCSPIM, it is automatically assembled. To simulate your
program, choose Simulator - Go, the Run Parameters box of Figure 3b will come up. Make sure
the box for Check for undefined symbols box is not checked and that the starting address is
0x00400000 (starting address of text segment).
Fi 3
PCS i Si l t S tti Wi d
Figure 3b, PCSpim Simulator Settings Window
pf3
pf4

Partial preview of the text

Download MIPS Assembly and PCSPIM: Logic Operations and Programming - Prof. Thomas E. Murphy and more Study notes Engineering in PDF only on Docsity!

ENGR 2030 MIPS Assembly, PCSPIM

What we will cover PCSPIM Logic Operations MIPS Assembly programming

Goals Become familiar with MIPS assembly language programming and PCSPIM.

PCSPIM PCSPIM is a MIPS assembler-simulator. The word size for the MIPS hardware is 32 bits (4-bytes). PCSPIM MIPS assembly programs must end with a system call to terminate the program and a blank line should be left at the end of the program (this is necessary due to a bug in some versions of PCSPIM). li $v0,10 # system call to stop simulator syscall

Pseudo Instructions Our MIPS assembler-simulator supports pseudo instructions as well as native instructions. The MIPS hardware we are working with supports only two conditional branches: beq and bne. However, our MIPS assembler-simulator also supports the pseudo instructions: bgt, blt, bge, and ble. These pseudo instruction conditional branches are translated into native instructions by the assembler. MIPS can simulate different addressing modes similarly.

The pseudo instruction conditional branches are converted to a slt (set if less than) followed by either a beq or bne, for example ble $t8, $t9, label # branch if R8 <= R is translated into the native instructions slt $t5, $t9, $t8 # set R5 if R9 < R beq $t5, $zero, label # branch if R5 = 0

Simulating Programs in PCSPIM The PCSPIM simulator expects the data segment to start at 0x10010000 and the text or program segment to start at 0x00400000.

Before assembling and simulating a program, make sure the MIPS settings allow pseudo instructions and do not use the trap file (exceptions). Under Simulator Settings, make sure the box to Allow pseudo instructions is checked and the box for Load trap (exception) file is not, see Figure 3a.

Once a program is loaded (File – Open) into PCSPIM, it is automatically assembled. To simulate your program, choose Simulator - Go, the Run Parameters box of Figure 3b will come up. Make sure the box for Check for undefined symbols box is not checked and that the starting address is 0x00400000 (starting address of text segment).

Figure 3b, PCSpim Simulator Settings Window

Logic Operations Need ways to clear a register, set a register to all ones, toggle each bit in a register, or extract a portion of a register. These types of logic operations (clearing, setting, toggling bits) are commonly used to enable hardware systems like interrupts and I/O devices. Masking is commonly used to extract a portion of a register.

To clear a register we want to have all bits equal to zero. Need an operation that performed on any data results in all zeros (AND with all zeros). For example ANDI $t0, $t0, 0x00000000 # clear t ANDI $t0, $t0, 0xFFFF0000 # clear low half t ANDI $t4, $t4, 0xFBFFFFFF # clear bit 26

To set bits in a register we want to have the particular bit set to one. Need an operation that performed on any data results in logical one (OR the bit with one). For example ORI $t1, $t1, 0xFFFFFFFF # set all bits of t ORI $t1, $t1, 0xFFFF0000 # set high half t ORI $t4, $t4, 0x00008000 # set bit 15

To toggle bits in a register we want to change 0s to 1s and 1s to 0s. Need an operation that complements particular bits (XOR the bit with one). For example XORI $t2, $t2, 0xFFFFFFFF # toggle all bits of t XORI $t2, $t2, 0xFFFF0000 # toggle high half t XORI $t4, $t4,0x00007D00 # toggle bits 13 to 18

Masks are typically used to extract portions of data, so we need to keep part of the data and eliminate (zero) the rest of the data. Masking is an AND operation with 1s for part want to keep and with 0s for part to eliminate. For example ANDI $t3, $t3, 0xFF000000 # extract high byte ANDI $t3, $t3, 0x000000FF # extract low byte

These examples assume that each logic operation has an immediate addressed version. If an immediate version does not exist, the constant would need to be created in a register and then use a register operation version, for example since our MIPS does not have an XOR immediate XORI $t2, $t2, 0xFFFFFFFF # toggle all bits of t Could be done using ORI $t3, $zero, 0xFFFFFFFF # set t3 to all 1s XOR $t2, $t2, $t3 # toggle all bits of t

Summarizing, To set a bit (1): OR with 1 To clear a bit (0): AND with 0 To toggle a bit: XOR with 1 To leave a bit unchanged: AND with 1, OR with 0, XOR with 0

MIPS Logic Examples Write a MIPS assembly program segment to determine if bit 5 of memory address test is set .data test .word 0x00360A

.text lw $t0, test($zero) andi $t0, $t0, 0x00000020 # mask bit 5 addi $t1, $zero, 0x00000020 # comparison value beq $t0, $t1, set5 # branch if bit 5 set

b2: .word 4 # b2 is 1/

.text add $t7,$zero,$zero # use t7 as x and y offset add $t8,$zero,$zero # use t8 as counter lw $t9, len

loop: lw $t1 x($t7) # load x[n] addi $t7, $t7, - lw $t2 x($t7) # load x[n-1] addi $t7, $t7, - lw $t3 x($t7) # load x[n-2]]

lw $t4, b div %t1, $t1, $t4 # box[n] lw $t4, b div %t2, $t2, $t4 # b1x[n-1] lw $t4, b div %t3, $t3, $t4 # b2*x[n-2]

add $t0, $t1, $t2 # y[n] = b0x[n] + b1x[n-1] + b2x[n-2] add $t0, $t0, $t addi $t7, $t7, 8 sw $t0, y($t7)

addi $t7, $t7, 4 # offset for next n addi $t8, $t8, 1 # update count

beq $t8,$t9,done # check if last n j loop

done: li $v0,10 # terminate the program syscall