arm assembly language examples

Upload: abhi198808

Post on 13-Jan-2016

12 views

Category:

Documents


0 download

DESCRIPTION

In this PPT various Assembler directives are shown for the ARM Processor, and Examples of how to convert C languge statements to ARM Assembly is given.

TRANSCRIPT

ARM assembly language examples

ARM assembly language examples

C to ARM AssemblerC:z = (a R0 and i = 0start CMP R0, #15 ; is i < 15?ADDLT R1, R1, R1 ; j = j + jADDLT R0, R0, #1 ; i++BLT startC:if (a < b) { x = 5; y = c + d; } else x = c - d;ADR r4,a ; get address for aLDR r0,[r4] ; get value of aADR r4,b ; get address for bLDR r1,[r4] ; get value for bCMP r0,r1 ; compare a < bBGE fblock ; if a >= b, branch to false blockfblock ADR r4,c ; get address for cLDR r0,[r4] ; get value of cADR r4,d ; get address for dLDR r1,[r4] ; get value for dSUB r0,r0,r1 ; compute a-bADR r4,x ; get address for xSTR r0,[r4] ; store value of xafterMOV r0,#5 ; generate value for xADR r4,x ; get address for xSTR r0,[r4] ; store xADR r4,c ; get address for cLDR r0,[r4] ; get value of cADR r4,d ; get address for dLDR r1,[r4] ; get value of dADD r0,r0,r1 ; compute yADR r4,y ; get address for ySTR r0,[r4] ; store yB after ; branch around false blockStructure of Assembly

AREA prog1, CODE, READONLY ; Name this block of code as prog 1ENTRYStartmov r0,#10mov r1,#3add r0,r0,#1stopb stop endAssembler Directives AREA: Define a block of data or codeEx.AREA sectionname,{attr} {attr}

Sectionname: is the name that the section is given.

Attr:ALIGN = expr the term expr can have any value from 0 to 31. The section is alined in 2^expr boundary.CODE: The section contains machine instructions. READONLY is the default.COMMON: This indicates a common data section.DATA : The section contains data not instructions. READWRITE is the default.READONLY: This indicates that this section should not be written , and it can therefore , be placed in read-only memory.READWRITE: This section can be read from and written to, and it must therefore be placed in read-write memory.Assembler DirectivesRN: Register name definitionThe RN directive defines a register name for a specified register.Syntax:Name RN exprWhere name is the name to be assigned to the register. RN is the directiveExpr evaluates to a number from 0 to 15.Example:Coeff1 RN 8; R8 as a coeff1Dest RN R0; defines dest for register 6.

Assembler DirectivesEQU Equate a symbol to a numeric constantThe EQU directive gives a symbolic name to a numeric constant, a register-relative value or a program-relative value. This directive is similar to #define to define constant in C.Syntax:Name EQU expr, {type}Where name is the Symbolic nameEQU is the directiveExpr is a register relative address, an absolute address, or 32-bit integer constant. Type is a optional.Example:SRAM_BASE EQU 0x4000 0000abc EQU 2XYZ EQU label+8Assembler DirectivesENTRY Declare an entry pointThe ENTRY directive declares an entry point to a program Syntax:ENTRYYou must specify at least one ENTRY point for a program. If no ENTRY exists, a warning is generated at link time.

Example:AREA prog1, code , readonlyENTRYAssembler DirectivesDCD, DCW and DCD- Allocate Memory and specify contentsThe DCB directive allocates one or more byte of memory DCW directive allocates one or more halfworlds of memoryDCD directive allocates one or more words of memory.Syntax:{label} DCB expr, {expr{label} DCW expr, {expr{label} DCD expr, {exprExample:Coeff DCW 0xFE37,0x8ECCC_string DCB C_String,0Assembler DirectivesALIGN- ALIGN data or code to appropriate boundriesThe ALIGN directive aligns the current location to a specified boundary by padding with zeros or NOP instructionsSyntax:ALIGN {expr{,offset}}Example:ALIGN 4 will align on 4-byte boundaryALIGN 8 will align on 8-byte boundaryAssembler DirectivesSPACE- RESERVE a block of memoryThe space directive reserves zeroed block of memory.Syntax:label SPACE exprExample:Data1 SPACE 255; defines 255 bytes of zeroed storage.

END :END of a source file.