22.06.2013 Views

Dott. Salvatore Pontarelli

Dott. Salvatore Pontarelli

Dott. Salvatore Pontarelli

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

giovedì 7 gennaio 2010<br />

Esercitazione n. 3<br />

<strong>Dott</strong>. <strong>Salvatore</strong> <strong>Pontarelli</strong>


giovedì 7 gennaio 2010<br />

Struttura di un modulo Assembly<br />

• Assembly è il linguaggio che l’ARM assembler<br />

(armasm) legge per produrre il codice oggetto.<br />

• può essere:<br />

• ARM assembly language<br />

• Thumb assembly language<br />

• un insieme dei due


giovedì 7 gennaio 2010<br />

Struttura di un modulo Assembly<br />

• la forma generale di una linea di codice<br />

assembly è la seguente:<br />

{label} {instruction|directive|pseudo-instruction}<br />

{;comment}<br />

• mnemonici, direttive e nomi dei registri<br />

possono essere scritti in minuscolo o<br />

maiuscolo, non mischiati


giovedì 7 gennaio 2010<br />

Label<br />

• Le Label sono simboli che rappresentano degli indirizzi. Durante la fase di<br />

assemblaggio viene calcolato l’indirizzo da associare ad una label.<br />

• L’indirizzo è calcolato come spiazzamento relativo rispetto all’inizio della<br />

sezione in cui è definita.<br />

• Le Label possono :<br />

• essere utilizzate per indirizzamento relativo rispetto al program<br />

counter<br />

• essere definite in una mappa (MAP and FIELD directives).<br />

L’origine della mappa può essre associata ad un registro in fase di<br />

esecuzione del codice. Questo consente un indirizzamento relaivo<br />

rispetto ad un registro.<br />

• Gli indirizzi delle label definite in altre sezioni sono calcolte durante la fase<br />

di link.


giovedì 7 gennaio 2010<br />

Constanti<br />

Possono essere numeric, boolean, character or string:<br />

• Le constanti numeriche possono essere:<br />

• Decimal, per esempio, 123<br />

• Hexadecimal, per esempio,, 0x7B<br />

• n_xxx dove:<br />

• n è una base tra 2 e 9<br />

• xxx è un numero espesso in base n<br />

• Caratteri Boolean: Le costanti TRUE e FALSE sono scritte {TRUE} e {FALSE}.<br />

• Caratteri: sono racchiusi tra apici singoli ( e usano lo gli escape characters del<br />

C)<br />

• Stringhe : sono racchiuse tra doppi apici


2.3.2 An example ARM assembly language module<br />

start<br />

stop<br />

Example 2-1 illustrates some of the core constituents of an assembly language module.<br />

The example is written in ARM assembly language. It is supplied as armex.s in the<br />

examples\asm subdirectory of ADS. Refer to Code examples on page 2-2 for instructions<br />

on how to assemble, link, and execute the example.<br />

Esempio di modulo ARM<br />

giovedì 7 gennaio 2010<br />

The constituent parts of this example are described in more detail in the following<br />

sections.<br />

AREA ARMex, CODE, READONLY<br />

; Name this block of code ARMex<br />

ENTRY ; Mark first instruction to execute<br />

MOV r0, #10 ; Set up parameters<br />

MOV r1, #3<br />

ADD r0, r0, r1 ; r0 = r0 + r1<br />

MOV r0, #0x18 ; angel_SWIreason_ReportException<br />

LDR r1, =0x20026 ; ADP_Stopped_ApplicationExit<br />

SWI 0x123456 ; ARM semihosting SWI<br />

END ; Mark end of file<br />

ELF sections and the AREA directive<br />

Example 2-1<br />

ELF sections are independent, named, indivisible sequences of code or data. A single<br />

code section is the minimum required to produce an application.<br />

The output of an assembly or compilation can include:<br />

• One or more code sections. These are usually read-only sections.<br />

• One or more data sections. These are usually read-write sections. They may be


giovedì 7 gennaio 2010<br />

Sezioni ELF<br />

• Le sezioni ELF sono sequenze indipendenti e<br />

indivisibili di codice o dati. Un’applicazione richiede<br />

almeno una sequenza di codice.<br />

• L’output dell’assemblatore include:<br />

• Una o più sezioni di codice (di solito read-only)<br />

• Una o più sezioni di dati (read-write).<br />

• L’inizio di una sezione è definito dalla direttiva<br />

AREA. Da il nome alla sezione e definisce i sui<br />

attributi.


giovedì 7 gennaio 2010<br />

Esempio di modulo ARM<br />

La direttiva Entry definisce la prima<br />

istruzione da eseguire.<br />

Il codice comincia dunque dalla label start.<br />

Carica i valori decimali10 e 3 nei registri r0 e<br />

r1, esegue la somma è mette il contenuto in<br />

r0.


giovedì 7 gennaio 2010<br />

Termine dell’esecuzione<br />

Dopo l’esecuzione del codice, l’applicazione<br />

terminate restituendo il controllo al<br />

debugger.<br />

(usa l’ARM semihosting SWI)<br />

La direttiva END segnala all’assembler la fine<br />

del codice sorgente da assemblare


giovedì 7 gennaio 2010<br />

Subroutine<br />

• Per eseguire le subroutine si usano le istruzioni di branch e link.<br />

• BL destinazione<br />

destinazione è la label della prima istruzione della subroutine.<br />

(può essere program-relative or register-relative)<br />

• L’istruzione BL:<br />

• mette l’indirizzo di ritorno nel link register (lr)<br />

• nette in pc l’indirizzo della subroutine<br />

• Dopo l’esecuzione della subroutine MOV pc,lr torna al chiamante.<br />

Per convenzione, i registri da r0 ad r3 sono usati per passare i<br />

parametri alla subroutine e per passare i risultati al chiamante


giovedì 7 gennaio 2010<br />

After the subroutine code is executed you can use a MOV pc,lr instruction to return. By<br />

convention, registers r0 to r3 are used to pass parameters to subroutines, and to pass<br />

results back to the callers.<br />

Esempio<br />

Note<br />

Subroutine<br />

Calls between separately assembled or compiled modules must comply with the<br />

restrictions and conventions defined by the procedure call standard. Refer to the Using<br />

the Procedure Call Standard in ADS Developer Guide for more information.<br />

Example 2-2 shows a subroutine that adds the values of its two parameters and returns<br />

a result in r0. It is supplied as subrout.s in the examples\asm subdirectory of ADS. Refer<br />

to Code examples on page 2-2 for instructions on how to assemble, link, and execute the<br />

example.<br />

AREA subrout, CODE, READONLY<br />

; Name this block of code<br />

ENTRY ; Mark first instruction to execute<br />

start MOV r0, #10 ; Set up parameters<br />

MOV r1, #3<br />

BL doadd ; Call subroutine<br />

stop MOV r0, #0x18 ; angel_SWIreason_ReportException<br />

LDR r1, =0x20026 ; ADP_Stopped_ApplicationExit<br />

SWI 0x123456 ; ARM semihosting SWI<br />

doadd ADD r0, r0, r1 ; Subroutine code<br />

MOV pc, lr ; Return from subroutine<br />

END ; Mark end of file<br />

Example 2-2<br />

ARM DUI 0068B Copyright © 2000, 2001 ARM Limited. All rights reserved. 2-17


giovedì 7 gennaio 2010<br />

Modulo Thumb<br />

Direttive CODE32 e CODE16<br />

Segnalano all’assembler se le istruzioni seguenti sono<br />

ARM (CODE32) o Thumb (CODE16).<br />

N.B.: Modificano lo stato dell’assembler non del<br />

processore.<br />

istruzione BX<br />

E’ un istruzione di branch che cambia lo sato del<br />

processore. Il bit meno significativo dell’indirizzo segnala<br />

se dopo il salto ci sono istruzioni ARM (0) o Thumb (1).


Writing ARM and Thumb Assembly Language<br />

2.3.4 An example Thumb assembly language module<br />

Modulo Thumb<br />

Example 2-3 illustrates some of the core constituents of a Thumb assembly language<br />

module. It is based on subrout.s. It is supplied as thumbsub.s in the examples\asm<br />

subdirectory of the ADS. Refer to Code examples on page 2-2 for instructions on how<br />

to assemble, link, and execute the example.<br />

AREA ThumbSub, CODE, READONLY ; Name this block of code<br />

ENTRY ; Mark first instruction to execute<br />

CODE32 ; Subsequent instructions are ARM<br />

header ADR r0, start + 1 ; Processor starts in ARM state,<br />

BX r0 ; so small ARM code header used<br />

; to call Thumb main program<br />

CODE16 ; Subsequent instructions are Thumb<br />

start<br />

MOV r0, #10 ; Set up parameters<br />

MOV r1, #3<br />

BL doadd ; Call subroutine<br />

stop<br />

MOV r0, #0x18 ; angel_SWIreason_ReportException<br />

LDR r1, =0x20026 ; ADP_Stopped_ApplicationExit<br />

SWI 0xAB ; Thumb semihosting SWI<br />

doadd<br />

ADD r0, r0, r1 ; Subroutine code<br />

MOV pc, lr ; Return from subroutine<br />

END ; Mark end of file<br />

giovedì 7 gennaio 2010<br />

CODE32 and CODE16 directives<br />

Example 2-3<br />

These directives instruct the assembler to assemble subsequent instructions as ARM

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!