You are on page 1of 34

HANOI UNIVERSITY OF SCIENCE AND TECHNOLOGY

SCHOOL OF INFORMATION COMMUNICATION AND TECHNOLOGY

FINAL PROJECT REPORT


Assembly Language and Computer Architecture
Group 2

Instructor Dr. Nguyễn Thị Thanh Nga


Class 122158

Students:
Đặng Quốc Tú 20194871
Nguyễn Duy Tiến 20194857
Bùi Mạnh Tú 20194870

Hà Nội, Ngày 8 Tháng 6 Năm 2021


Table of Contents
1. Đặng Quốc Tú 20194871 ........................................................................2
1.1. Project 1.10 .......................................................................................2
1.2. Project 2.5 .........................................................................................3
2. Nguyễn Duy Tiến 20194857 ...................................................................8
2.1. Project 1.23 (Bonus) .........................................................................8
2.2. Project 2.06 .....................................................................................14
3. Bùi Mạnh Tú 20194870 ........................................................................22
3.1. Project 1.19 .....................................................................................22
3.2. Project 2.3 .......................................................................................26

1
1. Đặng Quốc Tú 20194871
1.1. Project 1.10
1.1.1. Project requirements
Write a program that gets an integer i from the user and creates the table shown
below on the screen (example inputs provided). Subroutines are required for power, square,
and hexadecimal (in 32 bits arithmetic, attend to overflowed results). Hint: Hexadecimal
can be done with shifts and masks because the size is 32 bits.
i power(2,i) square(i) Hexadecimal(i)
10 1024 100 0xA
7 128 49 0x7
16 65536 256 0x10
1.1.2. Solutions
To limited to be used to enter a user data error (for example user try to instead string
not a number). The program has the function to check string input then conver to integer.

// Power function
while (exp != 0){
result = result * base;
exp--;
}
// Convert string number to number
for (int i = 0; str[i] != ‘\0’; i++){
if (first == 0) {
num = str[i] – ‘0’;
first++;
} else num = num*10 + str[i] – ‘0’;
}
1.1.3. Simulation results

2
1.2. Project 2.5
1.2.1. Project requirements
Viết chương trình tính giá trị biểu thức bất kỳ bằng phương pháp duyệt biểu thức
hậu tố. Các yêu cầu cụ thể:
• Nhập vào biểu thức trung tố, ví dụ: 9 + 2 + 8 * 6
• In ra biểu thức ở dạng hậu tố, ví dụ: 9 2 + 8 6 * +
• Tính ra giá trị của biểu thức vừa nhập
• Các hằng số là số nguyên, trong phạm vi từ 0 → 99.
• Toán tử bao gồm phép cộng, trừ, nhân, chia lấy thương
1.2.2. Solutions
Postfix evaluation
While characters remain in the postfix string
1. Read a character
2. If the character is a digit, convert to int and push
3. If the character is an operator
• Pop the stack twice obtaining the two operands.
• Perform the operation.
• Push the result.
Pop the final value from the stack.

3
int evaluatePostfix (char* exp)
{
// Scan all characters one by one
for (i = 0; exp[i]; ++i) {
// If the scanned character is an operand (number
here)
// push it to the stack.
if (isdigit(exp[i]))
push (stack, exp[i] - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else {
int val1 = pop(stack);
int val2 = pop(stack);
switch (exp[i]) {
case '+': push (stack, val2 + val1); break;
case '-': push (stack, val2 - val1); break;
case '*': push (stack, val2 * val1); break;
case '/': push (stack, val2/val1); break;
}
}
}
return pop(stack);
}

4
Convert Infix to postfix.
While characters remain in the infix string
1. Read the next character in the infix string
2. If the character is an operand, append the character to the postfix
expression
3. If the character is an operator @
• While the stack is not empty and an operator of greater or equal priority
is on the stack
• Pop the stack and append the operator to the postfix
o Push @
4. If the character is a left parenthesis (
• Push the parenthesis onto the stack
5. If the character is a right parenthesis )
• While the top of the stack is not a matching left parenthesis (
• Pop the stack and append the operator to postfix.
• Pop the stack and discard the returned left parenthesis.
Pop any remaining items on the stack and append to postfix.

5
for (i = 0, k = -1; exp[i]; ++i) {
// If operand, add it to output.
if (isOperand(exp[i]))
exp[++k] = exp[i];
// If ‘(‘, push it to the stack.
else if (exp[i] == '(')
push (stack, exp[i]);
// If ‘)’, pop and output from the stack
// until ‘(‘ is encountered.
else if (exp[i] == ')') {
while (!isEmpty(stack) && peek(stack) !=
'(')
exp[++k] = pop(stack);
if (!isEmpty(stack) && peek(stack) != '(')
return -1; // invalid expression
else
pop(stack);
}
else { // an operator is encountered
while (!isEmpty(stack) && prec(exp[i]) <=
prec(peek(stack)))
exp[++k] = pop(stack);
push (stack, exp[i]);
}
}
// pop all the operators from the stack
while (!isEmpty(stack))
exp[++k] = pop (stack );

6
1.2.3. Simulation results

7
2. Nguyễn Duy Tiến 20194857
2.1. Project 1.23 (Bonus)
2.1.1. Project requirements
Surpassing words are English words for which the gap between each adjacent pair
of letters strictly increases. These gaps are computed without "wrapping around" from Z to
A.
For example:

Write a function to determine whether a word passed into a function is a surpassing


word. You can assume the word is made of only alphabetic characters and are separated by
whitespace. We will consider the empty string and a 1-character string to be a valid
surpassing word.
is_surpassing_word("superb") => True
is_surpassing_word("excellent") => False

2.1.2. Solutions
The main idea is to compare the gap between two consecutive pairs of characters. It
is executed by running a for loop to check sequentially from the first gap to the end. If there
exists any gap that smaller than the previous gap, false is returned. Else, if the for loop can
run till the end, a true value is returned.
The program has a menu interface to let user choose what they want to do. When
the string input is entered, all the letter from ‘A’ to ‘z’ in ASCII table will be converted to
uppercase for computing gaps later. That means user can enter any character without taking
care if it is lower/uppercase. However, if user enter an character not in ‘A’ to ‘z’ in ASCII
table (e.g a number or +-*/&?.’,), an dialog will popup to notify that the input is invalid.

8
Pseudo-code:
print ("Enter the string input: ");
scan (input);
for (i = 0; i < input.size(); i++){
// Convert all character to upper case
if (input[i] < 'A' || input[i] > 'z') {
// If not in range ‘A’ to ‘z’
print ("Invalid input"); -> invalid
printMenu();
}
// If lowercase -> convert to upper
if (input[i] >= 'a')
input[i] -= 32;
}
boolean checkIncrease(){
boolean check = true;
int t1 = abs (input[1] - input[0]) ;
// Stores initialize gap
for (i = 2; i < input.size; i++){
int t2 = abs (input[i] - input[i-1]);
// Stores next gap
if (t2 < t1) return false;
if (present < previous gap) return false;
// next gap will become present gap of next loop
t1 = t2;
}
return true; // the gap always increase
}

9
Source code:

10
11
2.1.3. Simulation results
• Initial interface:

12
• Enter input dialog:

• Return value dialog:

• After return value, the menu pop up again on Run I/O. Now I will try to enter other
test cases:

13
2.2. Project 2.06
2.2.1. Project requirements

Source code:
.data
CharPtr: .word 0 Bien con tro, tro toi kieu asciiz
BytePtr: .word 0 Bien con tro, tro toi kieu Byte
WordPtr: .word 0 Bien con tro, tro toi mang kieu Word

14
.kdata
Bien chua dia chi dau tien cua vung nho con trong
Sys_TheTopOfFree: .word 1
Vung khong gian tu do, dung de cap bo nho cho cac bien
con tro
Sys_MyFreeSpace:

.text
Khoi tao vung nho cap phat dong
jal SysInitMem

-----------------------
Cap phat cho bien con tro, gom 3 phan tu,moi phan
tu 1 byte
-----------------------
la $a0, CharPtr
addi $a1, $zero, 3
addi $a2, $zero, 1
jal malloc
-----------------------
Cap phat cho bien con tro, gom 6 phan tu, moi
phan tu 1 byte
-----------------------
la $a0, BytePtr
addi $a1, $zero, 6
addi $a2, $zero, 1
jal malloc
-----------------------
15
Cap phat cho bien con tro, gom 5 phan tu, moi
phan tu 4 byte
-----------------------
la $a0, WordPtr
addi $a1, $zero, 5
addi $a2, $zero, 4
jal malloc

lock: j lock
nop

------------------------------------------
Ham khoi tao cho viec cap phat dong
@param khong co
@detail Danh dau vi tri bat dau cua vung nho co the
cap phat duoc
------------------------------------------
SysInitMem: la $t9, Sys_TheTopOfFree Lay con tro
chua dau tien con trong, khoi tao
la $t7, Sys_MyFreeSpace Lay dia chi
dau tien con trong, khoi tao
sw $t7, 0($t9) Luu lai
jr $ra

------------------------------------------
Ham cap phat bo nho dong cho cac bien con tro
@param [in/out] $a0 Chua dia chi cua bien con tro
can cap phat

16
Khi ham ket thuc, dia chi
vung nho duoc cap phat se luu tru vao bien con tro
@param [in] $a1 So phan tu can cap phat
@param [in] $a2 Kich thuoc 1 phan tu, tinh
theo byte
@return $v0 Dia chi vung nho duoc cap
phat
------------------------------------------
malloc: la $t9, Sys_TheTopOfFree
lw $t8, 0($t9) Lay dia chi dau tien con
trong
sw $t8, 0($a0) Cat dia chi do vao bien
con tro
addi $v0, $t8, 0 Dong thoi la ket qua tra
ve cua ham
mul $t7, $a1,$a2 Tinh kich thuoc cua mang
can cap phat
add $t6, $t8, $t7 Tinh dia chi dau tien con
trong
sw $t6, 0($t9) Luu tro lai dia chi dau
tien do vao bien Sys_TheTopOfFree
jr $ra

2.2.2. Solutions
1. Ensure that address of a word must be divisible by 4.
To do this, I use .align. An align of 2 will align next data item by a specific
byte boundary.

2. Function to get the value of pointer variable

17
3. Function to get address of a pointer variable

4. Function to copy two strings

18
5. Function to allocate two-dimensional array

6. Function getArray[i][j] to get value at row i, column j in two-dimensional array

7. Function setArray[i][j] to set value for index row i, column j of two-dimensional array

19
2.2.3. Simulation results
• Initial interface:

• Choose 1: Allocate new two-dimensional array

Program will ask user to enter size of array they want to allocate, including number
of rows and number of columns
• Choose 2: Set Array[i][j]

20
User enter value from array[0][0] to array[row-1][col-1]
• Choose 3: Get Array[i][j]

First, user need to enter index i, j that they want to get the value at. The program
will return array[i][j]
• Choose 4: Exit Program

21
3. Bùi Mạnh Tú 20194870
3.1. Project 1.19
File: n1.19_g2_BuiManhTu.asm, utils.asm
3.1.1. Project requirements
Write a program that input some variable names. Check if variable names consist
only of English letters, digits and underscores and they cannot start with a digit.
Example
● For name = "var_1__Int", the output should be variableName(name) = true;
● For name = "qq-q", the output should be variableName(name) = false;
● For name = "2w2", the output should be variableName(name) = false.
3.1.2. Solutions
Check if the first character is a digit.
Loop through the bytes array:
• Check if a digit.
• Check if an uppercase/lowercase character.
• Check if an underscore.
• If not all above return false.

Pseudo-code:
if(input[0] >= '0' && input[0] <= '9')
goto Output
while((input[i] >= '0' && input[i] <= '9') || (input[i]
>= 'A' && input[i] <= 'Z') || (input[i] >= 'a' && input[i]
<= 'z') || input[i] == '_')
nextElement
Output:
output()

22
Source-code:

23
24
3.1.3. Simulation results
String input dialog:

On not satisfied:

Continue option dialog:

On satisfied:

25
3.2. Project 2.3
Files: n2.3_g2_BuiManhTu.asm
3.2.1. Project requirements
Chương trình sau sẽ đo tốc độ gõ bàn phím và hiển thị kết quả bằng 2 đèn led 7
đoạn. Nguyên tắc:
- Cho một đoạn văn bản mẫu, cố định sẵn trong mã nguồn. Ví dụ “bo mon ky thuat
may tinh”
- Sử dụng bộ định thời Timer (trong bộ giả lập Digi Lab Sim) để tạo ra khoảng thời
gian để đo. Đây là thời gian giữa 2 lần ngắt, chu kì ngắt.
- Trong thời khoảng đó, người dùng nhập các kí tự từ bàn phím. Ví dụ nhập “bo
mOn ky 5huat may tinh”. Chương trình cần phải đếm số kí tự đúng (trong ví dụ trên thì
người dùng gõ sai chữ O và 5) mà người dùng đã gõ và hiển thị lên các đèn led.

3.2.2. Solutions
There are two interuptions in this program:
- Key interuption: user type in
- Time interuption: 1 (one) second elapsed
When key interuption happens, number of words typed is incremented. The input is
stored in store variable. For every second elapsed, the speed counter is updated and
displayed to the LED SEGMENT and printed to console, then reset speed and time.
Moreover, if the typed key is Enter which indicates the end of input, it branchs to
check accuracy and display result to dialog and console.

26
Pseudo-code:
while (true) {
unsigned register int time = 0;
unsigned register int length = 0;
if(onKeyPressed)
if (line break 0x0A)
break
length++;
if (time == 1)
if (length != 0)
break
length = 0
time = 0
}
CheckAccuracy()
Display()

27
Source code:
1
2
3
3.2.3. Simulation results
Speed Display

Keyboard Input

4
Accuracy Display

Continue Option

THE END

You might also like