110 likes | 229 Views
This guide explores the concept of macros in assembly language programming for microcontrollers. A macro is a named grouping of instructions that expands into the original code during assembly. Key directives such as `macro`, `endm`, and examples like bank selection procedures are discussed. Additionally, the importance of context saving in interrupt service routines (ISRs) is highlighted, showcasing operations like pushing and popping data. The role of assembler directives in enhancing program flexibility and efficiency is also examined, providing a complete overview for programmers.
E N D
Macros • A macro is a grouping of instructions, defined by the programmer and given a name. • When the source code is assembled, the macro is expanded out into the original instructions that made it up. • The macro itself is contained within the directives macro and endm. • Eg: • Bank1 macro • Bsf STATUS,RP0 • endm
Puzzle • Give output of the following instructions • Bcf STATUS,RP0 ;Select bank0 • Movlw 33h • Movwf TRISA • Output???
Output • PORTA = 33h
Bank Seletion • Ensure you have chosen correct bank in data memory • In the above program, if you want to input some value in trisa, you should select bank 1 (bsf status,5).
Context saving (Intservice ISR) • ;ISR • ;PUSH OPERATION • movwf w_temp ;Copy W to W_TEMP register, • swapf status,0 ;Swap status to be saved into W • movwf status_temp ;Save status to STATUS_TEMP register • ;Interrupt Service Routine • ... • actual ISR goes here • ... • ;POP OPERATION • swapf status_temp,0 ;Swap nibbles in STATUS_TEMP register • ;and place result into W • movwf status ;Move W into STATUS register ;sets • ;bank to original state • swapf w_temp,1 ;Swap nibbles in W_TEMP and keep result in • ;W_TEMP • swapf w_temp,0 ;Swap nibbles in W_TEMP and place result into W • ... • clear interrupt flag(s) here • ... • retfie
Cblock assembler directive • Blnkcnt equ 20h • Num1 equ 21h • Num2 equ 22h • Counth equ 23h • Countl equ 24h • Cblock 20h • Blnkcnt • Num1 • Num2 • Counth • Countl • Endc =
Assembler directives • While the Assembler program is written for the target microcontroller, it has to be processed by the Assembler first. • To aid this process and make it more powerful and flexible, a way is needed of passing information and instructions to the Assembler, which it recognizes as being for its attention only. • These instructions are called Assembler directives.