顯示具有 Embedded system 標籤的文章。 顯示所有文章
顯示具有 Embedded system 標籤的文章。 顯示所有文章

2015年10月11日 星期日

C assignments in ARM instructions - (A+B)-C

This article is about how to translate basic operation from C code to ARM assembly code.
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 codemeaning
ADDadd
AUBsubstract
ADRSet 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.








2015年9月22日 星期二

2.2.2 Assembly language

下列的ARM code是一個最基礎的組合語言

label1     ADR r4,c            
               LDR r0,[r4]              ; a comment            
               ADR r4,d            
               LDR r1,[r4]            
               SUB r0,r0,r1            ; another comment

組合語言通常有以下的特徵

  • 一個指令一行
  • Labels, 通常在first column給memory loacation名字
  • 指令必須從second column或second column之後開始,才能區分跟Label的不同
組合語言通常follow這樣的結構,如此才能方邊一行一行的分析。



上圖是ARM data processing instruction ,這個例子是ADD

  這個指令 ADDGT r0,r3,#5


  • cond   會依照f GT   condition  (1100) 來設定[註1]
  • GT的意思是Greater than (signed). (Z==0) && (N==V)
  • opcode   會顯示二進制 ADD 指令 (0100
  • 第一個operand  register Rn  會被設為 3 來代表 r3 ,
  • destination register Rd  會被設為0 for r0
  • the operand 2  field 會被設為 immediate value of 5.[註2]






[註1]cond 就是 Condition codes透過查表(請搜尋關鍵字ARM Cortex­A Series Programmer’s Guide for ARMv8­A Version: 1.0)

[註2]Many ARM and Thumb general data processing instructions have a flexible second operand. This is shown as Operand2 in the descriptions of the syntax of each instruction.Operand2 can be a:constantregister with optional shift.


Reference:
[1]Computers as Components - Principles of Embedded Computing Sy
stem Design Ch.2

2015年9月21日 星期一

2.2.1Computer architecture taxonomy

了解microprocessors之前我們必須先知道instruction sets 書本上的定義是 the programmer’s interface to the hardware. The instruction set is the key to analyzing the performance  of  programs.


電腦最基本的架構是什麼?  下圖就是一個最基本電腦的架構(圖例為von Neumann architectures)



計算機系統架構有最基本的
1. central processing unit (CPU)
2. memory- 包含了data還有instructions,當給定記憶體位置時可以被read, write
當一個計算機系統的memory包含了data及instructions就稱為von Neumann  manchine

CPU有許多內部的佔存器可以存取值
其中一種暫存器-program counter (PC),稱為暫存器那他存什麼呢? 存指令的地址(address)
舉例來說: CPU從memory擷取指令,經過解碼,再執行他。program counter並沒有直接的決定反而是間接的告訴這台機器下一個指令是什麼。透過改變指令我們可以決定CPU的動作


其中另一種可以取代von Neumann style的是Harvard architecture(幾乎跟von Newmann一樣老)
下圖即為Harvard 架構

In Harvard architecture, we can see that it has separate memories for data and program.
那麼Harvard架構中的Program Counter要指到哪裡呢? 他是指到program memory而不是data memory. 因此在這種架構中很難寫self-modifying programs(programs 寫進data值然後直接把值當作instruction)





Reference:
[1]Computers as Components - Principles of Embedded Computing Sy
stem Design Ch.2