ARM assembly pseudo-instructions MACRO and MEND

Publisher:温柔的爱情Latest update time:2018-04-15 Source: eefocusKeywords:ARM Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The MACRO pseudo-operation marks the beginning of the macro definition, and MEND marks the end of the macro definition.
Use MACRO and MEND to define a section of code, called a macro definition body, so that the code segment can be called multiple times through macro instructions in the program. The

syntax format is
MACRO
{$label} macroname {$parameter {,$parameter}...}
;code
...
;code
MEND

where:
$labelz When the macro instruction is expanded, label can be replaced with a corresponding symbol, usually a label. Use $ before a symbol to indicate that the corresponding value will be used to replace the symbol after $ when the program is assembled.
Macroname is the name of the defined macro, and
$parameter is the parameter of the macro instruction. When the macro instruction is expanded, it will be replaced with the corresponding value, similar to the formal parameter in the function. You can specify the corresponding default value for the parameter when defining the macro.
      MACRO
$HandlerLabel HANDLER $HandleLabel

$HandlerLabel
sub sp,sp,#4 ;decrement sp(to store jump address)
stmfd sp!,{r0} ;PUSH the work register to stack(lr does not push because it return to original address)
ldr r0,=$HandleLabel;load the address of HandleXXX to r0
ldr r0,[r0] ;load the contents(service routine start address) of HandleXXX
str r0, [sp,#4] ;store the contents(ISR) of HandleXXX to stack
ldmfd sp!,{r0,pc} ;POP the work register and pc(jump to ISR)
MEND

;;Call HandlerFIQ in the program
HANDLER HandleFIQ
HandlerIRQ HANDLER HandleIRQ
HandlerUndef HANDLER HandleUndef
HandlerSWI HANDLER HandleSWI
HandlerDabort HANDLER HandleDabort
HandlerPabort HANDLER HandlePabort
For example, the first one is used as an example to explain HandlerFIQ HANDLER HandleFIQ
;; After the program is assembled, the result of the macro expansion is
HandlerFIQ
sub sp,sp,#4    
stmfd sp!,{r0}  
ldr r0,=HandleFIQ
ldr r0,[r0]     
str r0,[sp,#4]      
ldmfd sp!,{r0,pc}    

Let's analyze it sentence by sentence. For the sake of analysis, assume sp = 0x33ff8000, $HandleLabel = 0x33ffff00,

[0x33ffff00] = 0x10000000, r0 = 0x56001234:

$HandlerLabel HANDLER $HandleLabel

The name of the macro is HANDLER and it has two parameters.

$HandlerLabel
defines a label

sub sp,sp,#4

Subtract 4 from the stack pointer to leave a word of space (for saving the jump address value), sp=0x33ff7ffc

stmfd sp!,{r0}

First, subtract 4 from sp (sp=0x33ff7ff8), and then push the r0 register to be used onto the stack. At this time, [0x33ff7ff8]=0x56001234

ldr r0,=$HandleLabel

Assign value to register r0, r0=0x33ffff00

ldr r0,[r0]  

Assign value to register r0, r0=0x10000000

str r0,[sp,#4]       

;Save register r0 to 0x33ff7ffc (0x33ff7ff8+4), sp is unchanged sp=0x33ff7ff8, if

str r0,[sp,#4]! sp changes

at this time

;[0x33ff7ffc] = 0x10000000

ldmfd sp!,{r0,pc}  

Pop the two words at the top of the stack and save them to r0 and pc respectively. At this time, sp=0x33ff8000, r0=0x56001234, pc=0x10000000. By comparison, it is not difficult to find that sp and r0 have not changed before and after execution, and the program jumps to 0x10000000 to execute.


Keywords:ARM Reference address:ARM assembly pseudo-instructions MACRO and MEND

Previous article:TQ2440 uboot---6.start.S in relocate part analysis, adr and ldr difference
Next article:Immediate value problem in ARM data processing instructions

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号