LC-3 Machine Code Programming Experiment

1. Purpose of the experiment

  1. Analyze and understand the problem to be solved as specified by the experiment.
  2. Use the machine code of LC-3 to design and implement related programs.
  3. Debug and run related programs through LC-3 emulator and get correct results.

2. Experimental content

Use the machine code of LC-3 to calculate how many bits are "1" in a 16-bit word. The program starts from x3000, the word to be calculated is stored in x3100, and the calculation result is stored in x3101.

3. Experimental procedures and results

1. Flow chart and ideas

insert image description here

Because the input data has only 16 bits, then only 16 cycles are needed to calculate the number of "1" in a data. The specific method to realize 16 rounds of loops is: set the value of a register (R2) to 16, decrement by 1 at the end of each round of loops, and stop the loop until R2 = 0. In this way, the loop can be performed 16 times, checking each bit. There is a problem here, the range of the 5-bit immediate value is limited, and the value of R2 cannot be added to 16 at one time. So I divided it twice: shilling R2 = 1, then R2 += 15.

A register (R3) is used to save the input data, and in each cycle, the sign bit is detected by judging whether the number is greater than or equal to 0. If the number is non-negative, the sign bit is 0; if the number is negative, the sign bit is 1. Record the information of each round of detection in the register (R1) that holds the result.

After each cycle, shift the value of R3 to the left by one bit. The specific method is to multiply the value of R3 by 2, which is realized by addition, that is, R3 = R3 + R3. In this way, the information of the highest bit can be updated, and the next round of detection can be performed until the end of the program.

2. Source code and comments

0011 000 000000000      ; 设置初始位置,PC = x3000

0101 001 001 1 00000    ; R1 <- 0 该寄存器用来记录1的个数
0001 010 001 1 00001    ; R2 <- 1 该寄存器用来判断循环次数
0001 010 010 1 01111    ; R2 <- R2 + 15 分两次进行赋值,共16次

0010 011 011111100		; R3 <- *(x3100) 将待测数据放进R3

						;循环
0000 011 000000001		; R3 >= 0 ? 判断R3符号位是否为0,是则跳过下一句
0001 001 001 1 00001	; R1 <- R1 + 1 更新R1的值
0001 010 010 1 11111	; R2 <- R2 - 1 
0000 010 000000010		; R2 == 0 ? 判断是否结束?如果R2为0则结束
0001 011 011 0 00011	; R3 <- R3 << 1
0000 111 111111010		; 向回跳转,循环继续

0011 001 011110110		; 将R1的结果存储至x3101

3. Experimental procedure and results

First edit the above code with LC3 Edit, and then generate the obj file, as shown in the figure, no errors were found.

insert image description here
Load it with the LC3 emulator. Write the input data at, and then execute
insert image description here
. x3100Finally x3101view the results here. Here I used four sets of test data tests.

Example 1: Input 15 (x000F)

insert image description here
The result is shown in the figure:
insert image description here
It can be seen that the complement of the input data has 4 "1", which is correct.

Example 2: Input 642 (x0282)

insert image description here
The result is shown in the figure:

insert image description here
It can be seen that the complement of the input data has 3 "1", which is correct.

Example 3: Enter 0 (x0000)

insert image description here
The result is shown in the figure:
insert image description here

Example 4: Input -100 (xFF9C)

insert image description here
The result is shown in the figure:
insert image description here
It can be seen that there are 12 "1"s in the complement of the input data, which is still correct.

Guess you like

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