IT3283E - Lab 10
IT3283E - Lab 10
Assignment 1:
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
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
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
5

Assignment 3:
# Guide
# Unit pixels 32
# Display pixels 256
.text
li a0, MONITOR_SCREEN # Load address of the display
loop:
addi t1, t1, 1 # i++
bgt t1, s1, exit # Exit when done with the last 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:
.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
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
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
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
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
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"
.text
main:
li t2, 512 # size
li t3, 128 # number of pixel
li t6, 64 #row size
li t4, 4 #unit
10

ff
ecall
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_ 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
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:
13

fi