LC-3 machine language counts how many bits are 1 in a 16-bit word

Experiment report of computer system 1

Table of contents

Topic description

AC code

Thought analysis


Topic description

Use LC-3 machine code to calculate how many bits are '1' in a 16-bit word.

The program starts at x3000.

The word to be calculated is stored in x3100.

The result of the calculation is stored in x3101.

AC code

0011000000000000;PC=x3000
0101000000100000;AND R0<-0
0101001001100000;AND R1<-0
0001001001101000;ADD R1<-8
0001001001000001;ADD R1<-R1+R1
0010010011111011;LD R2<-M[x3100]
0001001001100000;ADD R1<-R1+0
0000010000000110;BRZ x300D
0001010010100000;ADD R2<-R2+0
0000011000000001;BRZP x300A
0001000000100001;ADD R0<-R0+1
0001010010000010;ADD R2<-R2+R2
0001001001111111;ADD R1<-R1-1
0000111111111000;BRNZP x3005
0011000011110011;ST M[x3101]<-R0

Thought analysis

overall program design

The problem is to calculate how many bits are '1' in a 16-bit word. Think about it initially. To solve this problem, you need to count, determine whether it is '1', and a 16-time loop.

 

R0 is used as a counter, R1 controls the number of cycles, and R2 stores the word that needs to be judged.

First use the AND instruction to assign the value of R0 and R1 to 0, and then you need to assign the value of R1 to 16, because the immediate addressing mode imm5 of the ADD instruction can only represent -16 to 15, so you cannot directly assign 16 to R1, my The method is to first assign 8 to R1, that is, use the ADD instruction to make R0=R0+8, and then use the ADD instruction to make R0=R0+R0, so that the value of R0 can be set to 16. Then use the LD instruction to read the contents of the memory address x3100 into R2. The main problem is to calculate how many words are '1'. The method I take is to determine whether the value of R2 is positive or negative. If it is negative, then the first bit is '1', and then multiply it by 2, that is Move each of them to the left by one position, and repeat the judgment of the first position until the accumulative judgment has been made 16 times.

Finally, use the ST instruction to store the value of R0 into the memory unit whose address is x3101.

core data structure

There are three core variables, R0 is used as a counter, responsible for counting the number of 1s in the number, R1 controls the number of loops, decrementing from 16, R2 stores the word that needs to be judged, and adds it to itself after each loop. operate.

At the beginning of the program, first assign R0 and R1 to 0, that is, add 0, and then assign R1 to 8. After that, R1 adds itself to make the value become 16, and then assigns the memory address to x3100. Data is stored in R2.

In the main loop, count by judging whether R2 is a negative number. If it is a negative number, let R0 increment by itself, then add R2 to itself, and then let R1 decrement by itself.

Finally, the value of R0 is stored in the memory unit whose memory address is x3101.

Guess you like

Origin blog.csdn.net/weixin_62264287/article/details/126390903