DucHuy CA Lab2 2021
DucHuy CA Lab2 2021
Laboratory Session 2
.text
indicates that following items are stored in the user text segment, typically instructions
.data
indicates that following data items are stored in the data segment
.globl sym
declare that symbol sym is global and can be referenced from other files
2.2 Change the pseudoninstruction “li $t0, 5” in Lab2_2.s to “li $t0, -5”. What are the real
MIPS instructions for “li $t0, -5”. Explain how the real instructions work.
- lui $1, -1
➔ Load the upper 16 bits of register $1[R1] with the literal value of -1.
lui $1, 0xFFFF loads 0xFFFF0000 into $1
ori $t0, $1, 0xFFFB performs 0xFFFF0000 | 0x0000FFFB
The result is 0xFFFFFFFB, which is the two's complement representation of -5
- ori $8, $1, -5
➔ OR the lower 16 bits of that register with the literal value -5 and put it in $8.
OR the lower 16 bits of that register [R1=ffff0000] with the literal value -5 and put
result in $8 [R8] -> ffff0000 OR fffffffb => fffffffb in R8
2.3 Change the pseudoninstruction “li $t0, 5” in Lab2_2.s to “li $t0, 0xaabbccdd”. What are
the real instructions for “li $t0, 0xaabbccdd”. Explain how the real instructions work.
- lui $1, -21829
Load the upper 16 bits of register $1 with the literal value of -21829.
- LUI stands for "Load Upper Immediate" -21829 is the decimal representation of
0xAAAA (the upper 16 bits of 0xAABBCCDD) This instruction loads 0xAAAA into
the upper 16 bits of register $1
After lui $1, -21829: $1 = 1010101010101010 0000000000000000 (0xAAAA0000)
[R1=aabb0000]
3. Branching (35pts)
3.1 Load the assembly file Lab2_3.s into qtSpim and run. Try to win the game. What is the
secret number?
506 is the secret number
3.2 The figure 2 shows the output when player wins the game.
The assembly print both win and lose results. Fix the assembly and save it as Lab2_3.2.s
Code:
Ouput:
The secret number (506) is stored in $t0 at the beginning.
After reading the user's guess into $t1, we use a bne (branch if not equal) instruction to compare
it with the secret number.
If the guess is not equal to the secret number, it branches to the LOSE label.
If the branch is not taken (i.e., the guess is correct), it falls through to the win message.
After printing the win message, it jumps to the exit label to avoid falling through to the lose
message.
The LOSE label prints the lose message and then falls through to the exit.
The exit label uses syscall 10 to terminate the program.
This structure ensures that only one message (either "You win!!" or "You lose!!") is printed
based on whether the guess matches the secret number or not. The program then exits cleanly in
both cases.
3.3 Modify the game so that it will print out as follow (no iteration) using the instructions bgt,
or bge, or blt, or ble:
Code:
Output:
3.4 Modify the game so that player can keep guessing until he find the secret number. Save
your assembly as Lab2_3.4.s
Code:
Output:
3.5 Modify previous version so that player can decide to stop the game by input a flag. Save
your assembly as Lab2_3.5.s
Code:
Output:
4. String (15pts)
Assume that the input string contains all lowercase letters. The first letter of every word is
capitalized. Save your assembly as Lab2_4_1.s
Code:
Output:
+ Click build “debug” and observe the console window: Build finished
Summary some sentences of SECTIONS (Specify the sections allocation into memory: lines 88-
109
_________________
-Port 1: 0000ffe4 00000002
-P1IN: 00000020
________
-P1OUT: 00000021
- main:_ 0000c000
-WDTCL:_ 00000120
-LoopCtr: 416
-P1In and P1OUT registers are part of PORT_1_2 IN and OUT port on some micro controllers,
and they are used to read and write the input and output of the port, respectively
Step 5: When running and pausing, observe the Core Registers in Registers tab and please write
down comments about the transition of these registers in there.
Step 6: how to use breakpoint in CCS : Alt+Shift+Q,B
1. Open the Debug Perspective:
• To use breakpoints, you need to be in the Debug Perspective.
• Go to Window > Perspective > Open Perspective > Debug.
• If you are already in the Debug Perspective, you can proceed to the next step.
2. Set a Breakpoint:
• Source-level Breakpoint (based on code lines):
o Left-click in the left margin (to the left of the line number) where you want to set
the breakpoint.
o A blue dot will appear next to the line of code, indicating that a breakpoint is set.
Step 7: When running and pausing, click View and open Disassembly window, write down these
instructions of sample code:
Step 8: Based on the step 7, please explain the process of sample code under Computer
Architecture structure
Step 9: Change the time DELAYLOOPS to the period of LED1 is twice that of LED2.
Your code is changed (after successfully checked)
Explain the registers, address, memory, etc which will be changed to fulfill the new condition.
The code sets up a loop that blinks LED1 twice as fast as LED2. The loop counter
LoopCtr is incremented until it reaches the value of DELAYLOOPS (32000). Inside the
loop, LED1 is toggled every time the loop counter is incremented. LED2 is toggled every
other time the loop counter is incremented. To make LED1 blink twice as fast as LED2,
you need to change the loop condition so that LED1 is toggled twice as often as LED2.
This can be achieved by changing the loop condition to check if the loop counter is even
or odd.
Explain the registers, address, memory, etc which will be changed to fulfill the new condition.
-c000: 40B2 5A80 0120 MOV.W #0x5a80, &Watchdog_Timer_WDTCTL: The
Watchdog Timer control register is set to 0x5A80. This will disable the Watchdog Timer,
which is an important step to prevent the device from resetting prematurely.
-c006: 40F2 0041 0022 MOV.B #0x0041, &Port_1_2_P1DIR: The direction register for
Port 1 is set to 0x0041. This will configure the LED pins as outputs.
-c00c: 40F2 0041 0021 MOV.B #0x0041, &Port_1_2_P1OUT: The output register for
Port 1 is set to 0x0041. This will turn on the LEDs (the data bits will be set to 1)
-c012: 430F CLR.W R15: The R15 register is cleared to 0.
-c014: 903F 7D00 CMP.W #0x7d00, R15: The value in R15 is compared with 0x7D00. -
-c018: 3404 JGE ($C$L3): If the value in R15 is greater than or equal to 0x7D00 (which
is unlikely to occur in the first iteration), the program will jump to address $C$L3.
Reference:
1. https://en.wikibooks.org/wiki/MIPS_Assembly/Pseudoinstructions
2. https://courses.missouristate.edu/KenVollmar/MARS/Help/SyscallHelp.html
3. https://www.assemblylanguagetuts.com/mips-assembly-programming-
tutorials/#MIPS_Data_Types
4. https://en.wikibooks.org/wiki/MIPS_Assembly/Arithmetic_Instructions
5. https://gab.wallawalla.edu/~curt.nelson/cptr280/lecture/mips%20arithmetic%20instructio
ns.pdf