0% found this document useful (0 votes)
20 views13 pages

IT3283E - Lab 10

HUST Assembly lab report

Uploaded by

dcsptmsvcr
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)
20 views13 pages

IT3283E - Lab 10

HUST Assembly lab report

Uploaded by

dcsptmsvcr
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/ 13

Nguyễn Ngọc Hưng - 20226044

Báo cáo Computer Architecture


Lab 10

Assignment 1:

.eqv SEVENSEG_LEFT 0xFFFF0011 # Address of the LED on the left


# Bit 0 = segment a
# Bit 1 = segment b
# ...
# Bit 7 = dot sign
.eqv SEVENSEG_RIGHT 0xFFFF0010 # Address of the LED on the right
.text
main:
li a0, 0x66 # Set value for 7 segments
jal SHOW_7SEG_LEFT # Show the result
li a0, 0x66 # Set value for 7 segments
jal SHOW_7SEG_RIGHT # Show the result
exit:
li a7, 10
ecall
end_main:
# ---------------------------------------------------------------
# Function SHOW_7SEG_LEFT : Turn on/o the 7seg
# param[in] a0 value to shown
# remark t0 changed
# ---------------------------------------------------------------
SHOW_7SEG_LEFT:
li t0, SEVENSEG_LEFT # Assign port's address
sb a0, 0(t0) # Assign new value
jr ra
# ---------------------------------------------------------------
# Function SHOW_7SEG_RIGHT : Turn on/o the 7seg
# param[in] a0 value to shown
# remark t0 changed
# ---------------------------------------------------------------
SHOW_7SEG_RIGHT:
li t0, SEVENSEG_RIGHT # Assign port's address
sb a0, 0(t0) # Assign new value
jr ra

1

ff
ff
Assignment 2:

.data
prompt: .asciz "Enter a character: "
newline: .asciz "\n"
digit_map: .word 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F # 7-segment digit
map
.eqv sevseg_left 0xFFFF0011 # Address of the left 7-segment LED
.eqv sevseg_right 0xFFFF0010 # Address of the right 7-segment LED
.text
.globl main

main:
# Print prompt
li a7, 4 # syscall for print string
la a0, prompt # load address of prompt
ecall

# Read a character from the keyboard


li a7, 12 # syscall for read character
ecall
mv t0, a0 # Store the character in t0

# Get ASCII value and calculate last two digits


li t1, 10 # Divisor for modulo operation
rem t2, t0, t1 # t2 = t0 % 10 (last digit)
div t3, t0, t1 # t3 = t0/t1
rem t3, t3, t1 # t3 = t % 10 (second last digits)

# Display digits in 7-segment LEDs


# Display rst digit
la t5, digit_map # Load address of digit map
slli t3, t3, 2 #t3=t3*4
add t5, t5, t3 # Calculate address of rst digit in map
lb s1, 0(t5) # Load value of rst digit
li t6, sevseg_left # Load address of left 7-segment LED
sb s1, 0(t6) # Display rst digit

# Display second digit


la t5, digit_map # Load address of digit map
slli t2, t2, 2 #t3=t3*4
add t5, t5, t2 # Calculate address of second digit in map
lb s2, 0(t5) # Load value of second digit
li t6, sevseg_right # Load address of right 7-segment LED
sb s2, 0(t6) # Display second digit

2

fi
fi
fi
fi
# Exit program
li a7, 10 # syscall for exit
ecallThay đổi thông số đầu vào: a0 = 23, a1 = 10, a2 = 2024.

3

1. Print the Prompt

li a7, 4 # syscall for print string


la a0, prompt # load address of prompt
ecall
• This section uses a syscall (code 4) to print the prompt string to the screen. The
string's address is loaded into register a0, and the syscall is triggered.
2. Read a Character from the User

li a7, 12 # syscall for read character


ecall
mv t0, a0 # Store the character in t0
• The program reads a single character from the user using syscall 12 (read character).
The character is returned in a0and is saved into register t0 for later processing.
3. Calculate the Last Two Digits of the ASCII Value

li t1, 10 # Divisor for modulo operation


rem t2, t0, t1 # t2 = t0 % 10 (last digit)
div t3, t0, t1 # t3 = t0 / 10
rem t3, t3, t1 # t3 = t3 % 10 (second last digit)
• The ASCII value of the character is stored in t0.
• The program calculates the last digit of the ASCII value by using modulo (rem) with
10 and stores the result in t2.

4

• Then, it calculates the second-to-last digit by dividing t0 by 10, storing the quotient
in t3, and then taking the remainder when dividing t3 by 10.
4. Display the First Digit (Second-to-Last Digit) on the Left 7-segment

la t5, digit_map # Load address of digit map


slli t3, t3, 2 # t3 = t3 * 4 (each digit entry is 4
bytes)
add t5, t5, t3 # Add offset to the base address of
digit_map
lb s1, 0(t5) # Load the corresponding 7-segment
pattern
li t6, sevseg_left # Load address of left 7-segment LED
sb s1, 0(t6) # Display the pattern on the left 7-
segment
• The program loads the digit map base address into t5.
• It calculates the correct byte offset for the second-to-last digit by multiplying t3 by 4
(slli is a logical shift left by 2, equivalent to multiplication by 4).
• It adds the calculated offset to digit_map and loads the corresponding byte (7-
segment display pattern) into s1.
• The program then stores this byte at the sevseg_left address to display the digit
on the left 7-segment display.
5. Display the Second Digit (Last Digit) on the Right 7-segment

la t5, digit_map # Load address of digit map


slli t2, t2, 2 # t2 = t2 * 4 (same as before)
add t5, t5, t2 # Add offset to the base address of
digit_map
lb s2, 0(t5) # Load the corresponding 7-segment
pattern
li t6, sevseg_right # Load address of right 7-segment LED
sb s2, 0(t6) # Display the pattern on the right 7-
segment
• Similar to the previous step, the program loads the digit map, calculates the correct
offset for the last digit, and stores the corresponding 7-segment display pattern at the
sevseg_right address to show the digit on the right 7-segment display.

5

Assignment 3:

# Guide
# Unit pixels 32
# Display pixels 256

.eqv MONITOR_SCREEN 0x10010000 # Start address of the bitmap display


.eqv WHITE 0x00FFFFFF

.text
li a0, MONITOR_SCREEN # Load address of the display

li s1, 8 # Max amount of white on chessboard


li s2, WHITE
li t1, -1 # t1 = i

loop:
addi t1, t1, 1 # i++
bgt t1, s1, exit # Exit when done with the last row

andi t2, t1, 1 # Check eveness if even j = 0 else j = 1


bnez t2, odd_row

even_row:
# Draw at the even position
beq t2, s1, loop
slli t3, t1, 3
add t3, t3, t2
slli t3, t3, 2 # Store at 4( 8*i + j )
add t3, a0, t3

sw s2, 0(t3)
addi t2, t2, 2
j even_row

odd_row:
# Draw at the odd position
bgt t2, s1, loop
slli t3, t1, 3
add t3, t3, t2
slli t3, t3, 2 # Store at 4( 8*i + j )
add t3, a0, t3

sw s2, 0(t3)
addi t2, t2, 2
j odd_row
exit:

Explanation

6

1. Row Loop (loop):
◦ The program increments t1 (row counter).
◦ If t1 > 7 (all rows processed), it exits the loop.
2. Odd/Even Row Determination:

◦ andi t2, t1, 1 determines if the current row is even (t2=0) or odd
(t2=1).
◦ If even, the program enters the even_row section; otherwise, it goes to
odd_row.
3. Even Row:

◦ For an even row, white squares are drawn at even columns (j=0, 2,
4, ...).
◦ The address of each square is calculated as 4 * (8 * i + j) using
shifts and adds, where i is the row index and j is the column index.
4. Odd Row:

◦ For an odd row, white squares are drawn at odd columns (j=1, 3,
5, ...).
◦ The address calculation is the same as in the even row but with odd column
indices.

7

Assignment 4:

.eqv KEY_CODE 0xFFFF0004 # ASCII code from keyboard, 1 byte


.eqv KEY_READY 0xFFFF0000 # =1 if has a new keycode ?
# Auto clear after lw
.eqv DISPLAY_CODE 0xFFFF000C # ASCII code to show, 1 byte
.eqv DISPLAY_READY 0xFFFF0008 # =1 if the display is ready to do
# Auto clear after sw

.text
li a0, KEY_CODE # Load address for KEY_CODE
li a1, KEY_READY # Load address for KEY_READY
li s0, DISPLAY_CODE # Load address for DISPLAY_CODE
li s1, DISPLAY_READY # Load address for DISPLAY_READY

li t0, 0 # Initialize index for exit string check


li t3, 0 #Initial index for 'exit'
li t4, 4
li s2, 0
li s3, 1
li s4, 2
li s5, 3

loop:
WaitForKey:
lw t1, 0(a1) # Check if a key is ready
beq t1, zero, WaitForKey # If not ready, keep polling

ReadKey:
lw t1, 0(a0) # Read the key code into t1

process_key:
beq t3, s2, check_e # If t3=0, check e
beq t3, s3, check_x # If t3=1, check x
beq t3, s4, check_i
beq t3, s5, check_t

check_e:
li a3, 'e' #Load ASCII for 'e'
beq t1, a3, increase_exit
bne t1, a3, reset_exit
check_x:
li a3, 'x' #Load ASCII for 'x'
beq t1, a3, increase_exit
bne t1, a3, reset_exit
check_i:
li a3, 'i' #Load ASCII for 'i'
beq t1, a3, increase_exit
bne t1, a3, reset_exit
check_t:
li a3, 't' #Load ASCII for 't'
beq t1, a3, increase_exit
bne t1, a3, reset_exit

normal_check: # Check if lowercase (a-z)


li t5, 'a' # Load ASCII for 'a'
li t6, 'z' # Load ASCII for 'z'
blt t1, t5, check_upper # If t1 < 'a', check if it's uppercase
bgt t1, t6, check_digit # If t1 > 'z', check if it's a digit

8

is_lower:
# Convert lowercase to uppercase
addi t1, t1, -32 # Change to uppercase
j show_key

reset_exit:
li t3,0
j normal_check

increase_exit:
addi t3,t3,1
beq t3, t4, exit_program
j normal_check

check_upper:
# Check if uppercase (A-Z)
li t5, 'A' # Load ASCII for 'A'
li t6, 'Z' # Load ASCII for 'Z'
blt t1, t5, check_digit # If t1 < 'A', check if it's a digit
bgt t1, t6, check_other # If t1 > 'Z', check if it's other

# Convert uppercase to lowercase


addi t1, t1, 32 # Change to lowercase
j show_key

check_digit:
# Check if digit (0-9)
li t5, '0' # Load ASCII for '0'
li t6, '9' # Load ASCII for '9'
blt t1, t5, check_other # If t1 < '0', check if it's other
bgt t1, t6, check_other # If t1 > '9', check if it's other

# Display the same digit


j show_key

check_other:
# For any other character, display '*'
li t1, '*' # Set t1 to ASCII for '*'

show_key:
# Wait for display to be ready
WaitForDis:
lw t2, 0(s1) # Check if display is ready
beq t2, zero, WaitForDis # If not ready, keep polling

# Show the key


sw t1, 0(s0) # Write the processed key to display
j loop # Repeat the loop

exit_program:
li a7, 10 # syscall for exit
ecall # Exit the program

This RARS assembly program waits for a key press, processes the key, and displays the
result. It checks if the key is part of the "exit" sequence ("e", "x", "i", "t"), and exits if the
sequence is completed. If the key is a lowercase letter, it converts it to uppercase; if it's an
uppercase letter, it converts it to lowercase. The program also handles digits (0-9) and any

9

other characters are displayed as "*". The processed key is displayed on a screen, and the
program continues until the "exit" sequence is entered.

Assignment 5:

.data
PROMPT_X1: .ascii "Enter x1 coordinate (0 to 15): "
PROMPT_Y1: .ascii "Enter y1 coordinate (0 to 15): "
PROMPT_X2: .ascii "Enter x2 coordinate (0 to 15): "
PROMPT_Y2: .ascii "Enter y2 coordinate (0 to 15): "
INVALID_INPUT: .ascii "Invalid input. Please enter di erent coordinates.\n"

.eqv MONITOR_SCREEN 0x10010000 # Start address of the bitmap display


.eqv RED 0x00FF0000 # Red color
.eqv GREEN 0x0000FF00 # Green color

.text
main:
li t2, 512 # size
li t3, 128 # number of pixel
li t6, 64 #row size
li t4, 4 #unit

# Prompt user for x1


la a0, PROMPT_X1
li a7, 4 # Print string

10

ff
ecall

li a7, 5 # Read integer


ecall
mv s0, a0 # s0 = x1

# Prompt user for y1


la a0, PROMPT_Y1
li a7, 4 # Print string
ecall

li a7, 5 # Read integer


ecall
mv s1, a0 # s1 = y1

# Prompt user for x2


la a0, PROMPT_X2
li a7, 4 # Print string
ecall

li a7, 5 # Read integer


ecall
mv s2, a0 # s2 = x2

# Prompt user for y2


la a0, PROMPT_Y2
li a7, 4 # Print string
ecall

li a7, 5 # Read integer


ecall
mv s3, a0 # s3 = y2

# Validate that x1 != x2 and y1 != y2


bne s0, s2, check_y
j invalid_input

check_y:
bne s1, s3, draw_rectangle
j invalid_input

invalid_input:
la a0, INVALID_INPUT
li a7, 4 # Print string
ecall
j main # Restart the program

draw_rectangle:
# Determine rectangle boundaries
bgt s0, s2, set_x_min_max
mv s4, s2 # s4 = x_max
mv s5, s0 # s5 = x_min
j set_y_min_max

set_x_min_max:
mv s4, s0 # s4 = x_max
mv s5, s2 # s5 = x_min

11

set_y_min_max:
bgt s1, s3, set_y_min
mv s6, s3 # s6 = y_max
mv s7, s1 # s7 = y_min
j ll_rectangle

set_y_min:
mv s6, s1 # s6 = y_max
mv s7, s3 # s7 = y_min

ll_rectangle:
li a0, 0 # Counter for pixel position

ll_loop:
# Calculate current row and column
# Row = a0 / 64, Column = a0 % 64
div a1, a0, t6 # a1 = row index
rem a2, a0, t6 # a2 = column index (a0 % 64)
div a2, a2, t4

# Check if the current position is within the rectangle bounds


bge a1, s7, check_ ll # If row >= y_min, check ll
j skip_ ll

check_ ll:
ble a1, s6, check_col # If row <= y_max, check column
j skip_ ll

check_col:
blt a2, s5, skip_ ll # If column < x_min, skip
bgt a2, s4, skip_ ll # If column > x_max, skip
beq a1, s7, draw_red_pixel # Top border
beq a1, s6, draw_red_pixel # Bottom border
beq a2, s5, draw_red_pixel # Left border
beq a2, s4, draw_red_pixel # Right border

# Set pixel to green


li a3, MONITOR_SCREEN
add a3, a3, a0 # Address of pixel
li t0, GREEN
sw t0, 0(a3) # Store green color

skip_ ll:
addi a0, a0, 4 # Increment pixel counter
blt a0, t2, ll_loop # Continue loop if less than 512
j end_program

draw_red_pixel:
# Set pixel to green
li a3, MONITOR_SCREEN
add a3, a3, a0 # Address of pixel
li t0, RED
sw t0, 0(a3) # Store redcolor
j skip_ ll
end_program:
li a7, 10 # Exit program
ecall

12
fi
fi

fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
1. Input Validation:

◦ The program asks the user for two points (x1, y1) and (x2, y2), ensuring that
x1 != x2 and y1 != y2. If the input is invalid, the program prompts
the user to re-enter the coordinates.
2. Rectangle Calculation:

◦ The program computes the boundaries of the rectangle (min and max x and y
values) based on the user input. It ensures the correct ordering of coordinates
(from top-left to bottom-right).
3. Drawing the Rectangle:

◦ The program calculates the position of each pixel on the screen and checks if
it falls within the rectangle’s boundaries.
◦ Red Borders: The border pixels (top, bottom, left, right) are drawn in red.
◦ Green Interior: The pixels inside the rectangle (not on the borders) are
colored green.
4. Looping and Display:

◦ The program loops through all pixel positions (512 pixels in total, assuming a
64x64 bitmap), lling the rectangle with the appropriate colors.
5. Exit:

◦ Once the rectangle is drawn, the program exits.

13

fi

You might also like