0% found this document useful (0 votes)
24 views25 pages

DucHuy CA Lab2 2021

The document outlines a laboratory session for a Computer Architecture course, focusing on MIPS assembler directives, pseudo instructions, and branching in assembly programming. It includes practical exercises for loading assembly files, modifying instructions, and implementing game logic in MIPS. Additionally, it introduces Code Composer Studio for microcontroller programming, detailing project setup and memory allocation evaluation.

Uploaded by

Đức Huy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views25 pages

DucHuy CA Lab2 2021

The document outlines a laboratory session for a Computer Architecture course, focusing on MIPS assembler directives, pseudo instructions, and branching in assembly programming. It includes practical exercises for loading assembly files, modifying instructions, and implementing game logic in MIPS. Additionally, it introduces Code Composer Studio for microcontroller programming, detailing project setup and memory allocation evaluation.

Uploaded by

Đức Huy
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Computer Architecture Course: IT089IU

International University – VNU HCM Date: March 2021


Dr. Le Hai Duong & Dr. Ly Tu Nga Time: 6 hours

Laboratory Session 2

Duc Huy- ITITWE21116

I. Testing and Branching (70pts)


1. MIPS assembler directives

Figure 1 MIPS Memory Usage as viewed in SPIM

.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

Common data definitions


.word w1, ..., wn
store n 32-bit quantities in successive memory words
.half h1, ..., hn
store n 16-bit quantities in successive memory halfword
.byte b1, ..., bn
store n 8-bit quantities in successive memory bytes
.ascii str
store the string in memory but do not null-terminate it
- strings are represented in double-quotes “str”
- special characters, eg. \n, \t, follow C convention
.asciiz str
store the string in memory and null-terminate it
.float f1, ..., fn
store n floating point single precision numbers in successive memory locations
.double d1, ..., dn
store n floating point double precision numbers in successive memory locations
.space n
reserves n successive bytes of space
.align n
align the next datum on a 2n byte boundary.
For example, .align 2 aligns next value on a word boundary.
.align 0 turns off automatic alignment of .half, .word, etc. till next .data directive

2. Pseudo instructions (20pts)


Pseudo intructions do not correspond to real MIPS instructions. Assembler would translate
pseudo intructions to real instructions (one or more instructions). Pseudo instructions not
only make it easier to program, it can also add clarity to the program, by making the
intention of the programmer more clear.
2.1 Load the assembly file Lab2_2.s into qtSpim and run. What is the real instruction shown
in qtSpim for the pseudo instruction “li $t0, 5”?
- ori $8, $0, 5
- Ori is stand for OR 16 bits. $8 is register R8 [t0]. 5 is an interger
- ori $8, $0, 5 -> R8=5, because R0:0 -> 0000; 5 -> 0101; OR -> 0101 -> R8

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]

ori $8, $1, -13091


➔ OR the lower 16 bits of that register with the literal value -13091 and put it in $8.
ORI stands for "OR Immediate" -13091 is the decimal representation of 0xCCDD (the
lower 16 bits of 0xAABBCCDD) $8 is the same as $t0 in MIPS register naming This
instruction ORs the lower 16 bits of $1 (which are all zeros after the LUI) with
0xCCDD. After ori $8, $1, -13091: $8 = 1010101010101010 1100110011001101
(0xAABBCCDD) in register R8

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:

Save your file as Lab2_3.3.s

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)

Modify Lab2_4.s so that it converts an input string as follow:

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:

II. Introduction to CCS (30pts)


+Download and install the newest version of Code Composer Studio from:
https://software-dl.ti.com/ccs/esd/documents/ccs_downloads.html
+ Create the new project by Alt+Shift+N
+ In Target, type msp430g2553 and in project name please type yourfullname_lab1,
select Empty Project (with main.c)

+ In main.c, type the sample code of Table 1


Table 1. control LED under controlled Loop in MSP430
No. Test (Sample Code) Comments/Results/Functions
1. #include <msp430.h>
2
3 #define LED1 BIT0 Macro LED1 (assigned P1.0 or BIT0)
4 #define LED2 BIT6 Macro LED2 (assigned P1.6 or BIT6)
5 #define DELAYLOOPS 32000
6 /* main.c */
7 void main(void)
8 {
9 int LoopCtr; Stop watchdog timer.
10 WDTCTL = WDTPW | WDTHOLD;
11 Setup LED1 and LED2 to direction
12 P1DIR =LED1|LED2; output and turn on
13 P1OUT =LED1|LED2;
14
15 while(1){
16 for(LoopCtr=0;LoopCtr<DELAYLOOPS;++LoopCtr) empty delay loop
17 {
18 } toggle LEDs
19 P1OUT ^=LED1|LED2;
20 }
}

+ Click build “debug” and observe the console window: Build finished

+ Click Run Debug and select Run in background


Step 1: Evaluation memory allocation: click View> Memory Allocation
Fill in the percentage of RAM allocation: _______15%____ and files:
Fill in the percentage of FLASH allocation: 0% and files:

Step 2: Not run, click lnk_msp430g2253.cmd to fill in these answers.

Summary some sentences of SECTIONS (Specify the sections allocation into memory: lines 88-
109

Step 3: open test.map and fill in these addresses following:


address: 0000c02a

In Section Allocation MAP:

-text: 0000c000 00000050

-stack: 000003b0 00000050

-WDT: 0000fff4 00000002

_________________
-Port 1: 0000ffe4 00000002

-Reset: 0000fffe 00000002


Memory Configuration: INT02: 0000ffe4 00000002 00000002 00000000

Global symbols: sorted alphabetically by Name:

-P1IN: 00000020

________
-P1OUT: 00000021

- main:_ 0000c000

-WDTCL:_ 00000120

-LoopCtr: 416

How many symbols? 128

Step 4: The Value of registers:


P1IN: before ____0______ and after ____0x06_______
P1OUT: before 0 and after 0x41
LoopCtr: before 0 and after 416
Hint: you can use Expression or open the Core Registers tab (Port_1_2) to find the
solution
Explain the values of P1IN and P1OUT registers in Port_1_2:

-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

You might also like