LC-3 machine code programming experiment seeking grades

1. Purpose of the experiment

  1. Analyze and understand assigned problems to be solved.
  2. Use the assembly code of LC-3 to design and realize related programs.
  3. Debug and run related programs through LC-3 emulator and get correct results.

2. Experimental content

Sort the grades of the students using an array.

Background: A teacher needs your help to determine the student's grade. She wants to determine the student's final grade based on the student's rank in the class and the test score.

The specific grade requirements are as follows:

  • If the student's score is ranked in the top 25% of the class, and the test score is 85 points and above, the student can get an A
  • If a student cannot get an A, but ranks in the top 50% of the class with a test score of 75 or above, the student can get a B
  • The rest of the students are all C

specific sitiuation:

  • There are 16 students in total, and each student has only one grade
  • Write a program to sort grades for this class in assembly language.
  • Your program must sort the students' grades and then count the number of students who got A's and B's. Program starts with x3000

Input to the program:

  • Unranked grades of the 16 students in the class;
  • Each fraction is an integer from 0 to 100 represented by a 16-bit unsigned number
  • Scores are stored in 16 consecutive memory locations - one score per location; starting with x3200;
  • The last fraction is stored in x320F
  • You can assume that all scores are different from each other (each score is unique)

The output of the program (your program must have two outputs):

  • The scores of 16 students are sorted. Scores must be sorted in descending order in consecutive memory locations - one score per memory address, stored starting at x4000; i.e. x4000 stores the highest score
  • The number of students who received A's and B's. The number of students who got A's must be stored in x4100 and the number of students who got B's must be stored in x4101

3. Experimental procedures and results

1. The general idea and implementation of the experiment

Firstly, the original data of 16 scores should be input in memory x3200, and copied to x4000 through a round of loop.

Use bubble sort to sort in descending order on the new address to get the ranking. Finally, filter the array at x4000 to count the number of students who have obtained grade A and grade B. The overall flow chart is as follows:
insert image description here
the above is an abstract and general process, and the specific implementation method will be expanded below. The specific flowchart of each step will be provided below.

a) Complete the copy of 16 data

The flow chart of the algorithm is as follows:
insert image description here
It is easy to see that the cycle adopts the do-loop structure.

There are many places to pay attention to. For example, to assign a value to R3, due to the limitation of the immediate value in [-16, 15], it needs to be completed twice. And if you want to realize *R2 <- *R1, you can use two instructions LDR and STR to complete. Besides that, I use register R0 to hold the new address x4000 long-term. The assembly code is shown in the figure:

insert image description here

b) Bubble sort 16 data

In order to make the process easier to understand, first I implemented the algorithm using C++ language, as shown in the figure below: The
insert image description here
corresponding algorithm flow chart is as follows:
insert image description here
Because in the C++ language, I used a for loop. Then in the process of transferring it to assembly language, I adopted the Jump-to-middle structure. The assembly code is as follows:
insert image description here

c) Screening and rating of 16 data

Also for the convenience of understanding, first use C++ programming. Since there is no need to count the number of students who get C, the cycle can be terminated after 8 rounds. The code is as follows:
insert image description here
and then the flowchart.
insert image description here
In the process of assembly, complex expressions cannot be used for the if statement, so more conditional jumps are used, similar to the goto statement of C++. The implementation of the loop still adopts the jump-to-middle structure.

insert image description here
Since multiple comparisons with data such as 85 and 75 are involved, if subtraction is used, the operation of inverting and adding one to the register will be frequently performed, which is undoubtedly a waste of space-time resources. In order to solve this problem, only -85 and -75 are stored in the register, so that when comparing, they can be added directly (equivalent to subtraction), which is better.

2. Loading of assembly code

Still complete the editing in LC3Edit and generate the obj file, and load it into the LC3 simulator, as shown in the figure, save the original data of the students' grades in the location of x3200, and then the experiment can be carried out.
insert image description here

3. Code function test and correctness inspection.

Two representative examples are mainly used in the following. In these two groups of samples, there are no repeated values ​​and boundary values .

Example 1: 75 85 90 60 73 87 100 38 66 80 76 82 93 95 74 86

insert image description here
The running result is as shown in the figure below:
insert image description here
It can be seen that the data has been copied to x4000. The sorting function works fine.
insert image description here
The value at 4100 is 4 and the value at x4101 is 4. Explain that 4 students got A and 4 got B. This is consistent with the calculation results.

Example 2: 75 85 60 61 73 84 100 38 66 53 76 82 72 95 74 80

insert image description here
The running result is as shown in the figure below:
insert image description here
It can be seen that the sorted data can be seen at x4000.
insert image description here
At the same time, it can be seen at x4100 and x4101 that 3 people got A grades and 5 people got B grades. This result is in line with objective reality.

full code

;author: CAO-Wuhui
;date: 2021.5.17
;function: Sort students' grades
;
;
;
		.ORIG	x3000	;起始地址
;
;完成对原始数据的拷贝
;
		LD	R1	DATA	;R1指向源地址
		LD	R2	RESULT	;R2指向目的地址
		AND	R0	R0	#0	;R0长期指向目的地址
		ADD	R0	R0	R2		
		AND R3 R3 #0	
		ADD R3 R3 #1
		ADD R3 R3 #15	;R3 = 16,循环次数
;
;循环,do-loop结构,将输入数据拷贝到目的地址
;

INIT_LOOP	LDR	R4	R1	#0	;R4 = *R1
			STR R4	R2	#0	;*R2 = R4
;
			ADD	R1	R1	#1
			ADD R2	R2	#1
			ADD	R3	R3	#-1
			BRp	INIT_LOOP

;
;冒泡排序,双重循环均采用 jump-to-middle 结构
;

		AND	R1	R1	#0

		ADD	R1	R1	#1	;i = 1
		BRnzp MIDDLE1
LOOP1	AND	R2	R2	#0	;j = 0
		BRnzp	MIDDLE2	;

LOOP2	ADD	R3	R0	R2
		ADD	R4	R3	#1
		LDR	R5	R3	#0	;R5 <- *R3
		LDR	R6	R4	#0	;R6	<- *R4
		
		NOT	R7	R6
		ADD	R7	R7	#1
		ADD	R7	R7	R5
		BRzp	END2
;
;exchange arr[j] and arr[j + 1]
;
		STR	R5	R4	#0
		STR	R6	R3	#0

END2	ADD	R2	R2	#1
MIDDLE2	ADD	R3	R1	R2
		ADD	R3	R3	#-16
		BRn	LOOP2

		ADD	R1	R1	#1
MIDDLE1	ADD	R3	R1	#-16
		BRn	LOOP1

;
;排序结束
;以下开始筛选等级
;循环依旧采用 jump-to-middle 结构
;

		AND	R1	R1	#0	;i = 0
;
;初始化R6 = -85,R7 = -75
;
		LD	R6	A_GRADE	;
		LD	R7	B_GRADE	;
		NOT	R6	R6
		ADD	R6	R6	#1
		NOT	R7	R7
		ADD	R7	R7	#1
;
		AND	R2	R2	#0	;cntA = 0,A的人数
		AND	R3	R3	#0	;cntB = 0,B的人数
;
		BRnzp	MIDDLE3
LOOP3	ADD	R4	R1	#-4
		BRzp	IS_B
;
		ADD	R4	R0	R1	;R4 = arr[i]
		LDR	R4	R4	#0	;
		ADD	R5	R4	R6	;if
		BRn	IS_B
		ADD	R2	R2	#1	;cntA++
		BRnzp	NEXT
;
IS_B	ADD	R4	R0	R1	;R4 = arr[i]
		LDR	R4	R4	#0	;
		ADD	R5	R4	R7	;if
		BRn	NEXT
		ADD	R3	R3	#1	;cntB++
;		
NEXT	ADD	R1	R1	#1
MIDDLE3	ADD	R4	R1	#-8
		BRn	LOOP3
;
;最后将结果保存到指定位置
;
		LD	R1	A_CNT
		STR	R2	R1	#0
		LD	R1	B_CNT
		STR	R3	R1	#0
;
		HALT
;
DATA	.FILL	x3200
RESULT	.FILL	x4000
A_CNT	.FILL	x4100
B_CNT	.FILL	x4101
A_GRADE	.FILL	x0055		
B_GRADE	.FILL	x004B

.END 

Guess you like

Origin blog.csdn.net/weixin_46655675/article/details/130858363