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月23日 星期三

Image Processing Tutorial-histogram & Canny’s edge detection


The mission is to practice using histogram function in MATLAB,
In this MATLAB code turorial, we choose a gray level Lena image. Of course, you can use any picture you want.

Here are three practices we are going to do
(a)     Compute and plot its histogram.
(b)     Apply histogram equalization to obtain a transformed image.
(c)      Apply Canny’s edge detection method to the given image (with two sigma
        values of the Gaussian filter) to show the two edge maps that you obtain.

Procedure:

Practice (a)
By using the function ‘imhist’, a histogram of Lena can be shown. This histogram shows that the image has dark pixel rather than bright pixel. And there is no extremely bright pixels shown in this picture. 
Practice (b)
By calling the function ‘histeq’, the original image is adjusted to the one with obvious contrast. There are more white pixels shown in the new image. Besides, it is easier to identify every single part from the new image.
Practice (c)
At first, we apply 2 kinds of Gaussian filters with different sigma to the image. And then use Canny’s edge detection method on them. According to the theory, the bigger the sigma, more blurred will be the image. And it is also validated by the result shown above. From the picture we can see that the image with lower sigma can be identified more boundaries and edges. The image with higher sigma cannot be detected much silhouette.

Let's see how do we complete it by useing Matlab :)
1:  %%     
2:  %%%%%%%%%%%%%%%%%%%%%%% (a)      Compute and plot its histogram.  %%%%%%%%%%%%%%%%%%%%%%%  
3:  im_GrayLena=imread('431.JPG');  % read image  
4:  %figure,imshow(im_GrayLena);       % show image  
5:  %title('gray Lena');           % imgae title gray_lena  
6:  twoD_Lena=rgb2gray(im_GrayLena);    %convert to 2D  
7:  figure,imhist(twoD_Lena);        % process histogram and show  
8:  title('Histogram of Lena')         
9:  xlabel('Grayvalue')  
10:  ylabel('Probality Of Occurence')  
11:  %%  
12:  %%%%%%%%%%%%%%%%%%%%%%% (b) Apply histogram equalization to obtain a transformed image. %%%%%%%%%%%%%%%%%%%%%%%  
13:  histeq_lena = histeq(twoD_Lena);    % histogram equalization   
14:  figure, imshow(histeq_lena)       % show image after histogram equalization   
15:  title('Lena after Equalization')         
16:  figure,imhist(histeq_lena);    
17:  title('Histogram of Lena after Equalization')  % Histogram of Lena after Equalization   
18:  xlabel('Grayvalue')  
19:  ylabel('Probality Of Occurence')  
20:  %%  
21:  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
22:  %%%%%%%%%%%%%%%%(c)Apply Canny’s edge detection method to the given image                  %%%%%%  
23:  %%%%%%%%%%%%%%%%  (with two sigma values of the Gaussian filter)to show the two edge maps that you obtain.%%%%%%  
24:  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  
25:  I=twoD_Lena;  
26:  Iblur02 = imgaussfilt(I, 0.2);  
27:  figure,imshow(Iblur02);  
28:  title('Guassian Sigma=0.2');  
29:  BW_canny02=edge(Iblur02,'canny');  
30:  figure,imshow(BW_canny02);  
31:  title('Guassian&canny edge Sigma=0.2');  
32:  Iblur4 = imgaussfilt(I, 4);  
33:  figure,imshow(Iblur4);  
34:  title('Guassian Sigma=4');  
35:  BW_canny4=edge(Iblur4,'canny');  
36:  figure,imshow(BW_canny4);  
37:  title('Guassian&canny edge Sigma=4');  
38:  %figure;  
39:  %imshowpair(BW_canny1,BW_canny4, 'diff')  

The result is shown below





Please email me if you have any question
Email: HSC38@pitt.edu

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