By doing this practice, you will understand some basic instructions, such as ADD, SUB, ADR, etc.
Background knowledge: Arm is a load-store architecture, which means data operands must be store in the register first. After the arithmetic, Stored back the result to main memory. ARM can be indirect addressing.
So, let's translate it!
C code: x = (a +b) - c ;
The first thing we are going to do is getting the address of a, then ask a, "what's your value?"
And do the same thing to b and c.
The instruction we need is shown in the following chart.
| ARM code | meaning |
| ADD | add |
| AUB | substract |
| ADR | Set register to address |
| LDR | Load |
Register is like a temporary house for "address" or "value" to live.
So if you want to store the data from memory, give them a temporary house, register.
There are four operands a, b, c and x. therefore, we need four registers to stored them. Let's say, r0 (register 0) for a, r1 (register 1) for b, r2 (register 2) for c, r3 (register 3) for x.
We also need a register to store address which is reusable. Let's say r4 (register 4) for x.
1: ADR r4,a ; get address for a
2: LDR r0,[r4] ; get value of a
3: ADR r4,b ; get address for b, reusing r4
4: LDR r1,[r4] ; load value of b
5: ADD r3,r0,r1 ; set intermediate result for x to a + b
6: ADR r4,c ; get address for c
7: LDR r2,[r4] ; get value of c
8: SUB r3,r3,r2 ; complete computation of x
9: ADR r4,x ; get address for x
10: STR r3,[r4] ; store x at proper location
- From line 1 to 4, 6, 7 is to get address from variable to register, and then get the value which is store in the address.
- Line 5 is arithmetically plus r0 and r1 and store in r3. Line 8 is similar to line5.
- Line 10 is a little different, because the destination register is the second one, [r4].
- STR r3,[r4] would store the contents of r3 in the memory location whose address is given in r4.
Reference:
[1]computer as components 2nd addition.
nice job.
回覆刪除HAHA 全世界應該沒幾個人會看 謝謝你喔
刪除不客氣喔你~你繼續寫~我就繼續看囉~看到你不想寫時
刪除