assembly 08. outline local labels jump lengths external libraries macros 1
DESCRIPTION
Local Labels Label that starts with a period E.g.,.loop Local labels allow multiple definitions of same label name Local label is associated with previous (above) global label Local labels must be separated by at least one global label Local labels may exist within a procedure 2TRANSCRIPT
Assembly 08
2
Outline
• Local Labels• Jump Lengths• External Libraries• Macros
3
Local Labels
• Label that starts with a period • E.g., .loop
• Local labels allow multiple definitions of same label name
• Local label is associated with previous (above) global label
• Local labels must be separated by at least one global label
• Local labels may exist within a procedure
4
Local Labels
• Local labels are at least local to procedures in which defined
• Helpful to have global labels that are never used• Just for better local labels
• Local labels cannot be used as breakpoints for GBD
• Be careful about naming local labels• nasm doesn’t care about your intentions
5
Local LabelsA:
.loop:
.test:
B:.loop
.test:
- local labels belong to previous (above) global label
- local labels cannot be referenced before the global label that “owns” it
6
Local Labels
• Local Labels are extremely useful for labels in procedures• E.g.,
function_1:.loop:
function_2:.loop:
7
Outline
• Local Labels• Jump Lengths• External Libraries• Macros
8
Jump Lengths
• Have you ever received this error:“error: short jump is out of range.”
• This error occurs when the conditional jump instruction is “too far away” from the label it references
• (Note that this error will not happen for unconditional jumps)
9
Jump Lengths
• Three types of jumps in x86:
• Short Jump• Near Jump• Far Jump
• Note: “far jumps” are extremely rare
10
Short Jump
• Default conditional jump in x86• Jump target must be within 127 bytes• (If jump target > 127 bytes away, get an error)
• Only requires 2-byte opcode• Faster • Hence why default
• Usage:• <conditional jump> <label>• je _loop
11
Near Jump
• Jump target > 127 bytes away• But- jump target within same code segment
• I.e., within code’s memory
• Requires 4 to 6 byte opcode• Slightly less efficient than short
• Usage:• <conditional jump> near <label>;• je near loop;
12
Far Jump
• Jump target is beyond code segment• Extremely rare• (Will not use in this class)
code segment
other code
segment
far jump
13
Jump Lengths
• So, if you encounter “error: short jump is out of range”, use the near keyword:
je _loop ; If this produces an error,
je near _loop ; use near keyword
14
Outline
• Local Labels• Jump Lengths• External Libraries• Macros
15
External Code Libraries
• Helpful to put procedures into external file(s)• Known as a “library” or “module”
• Increases code readability, reusability, maintainability
• External library (or libraries) for common tasks:• E.g., read, write, exit, sort, etc.
16
External Libraries
• External module is .asm source file (w/ procedure defs)
• Gets assembled into .o file
• Gets linked with other .o file(s) to create executable
• Only one module contains _start• Similar to main() in C++
assembly source code
file(s)
External Libraries
.asm
.asm
.asm
assembler
.o
.o
.o
linker
executable
object file(s)
executable
program file
18
External Libraries
• Modules can “talk” to each other via procedure calls and data references• Similar to an interface
• Must use proper declarations of EXTERN and GLOBAL
• EXTERN -> procedure gets “imported” from somewhere else• GLOBAL -> procedure gets “exported” to somewhere else
19
External Library Example
section .textglobal _startextern print_err;
_start:call print_err;
mov eax, 1;mov ebx, 0;int 0x80;
section .textglobal print_err;msg: db “ERROR!!”,10;len: equ $-msg;
print_err:mov eax,4;mov ebx,1;mov ecx, msg;mov edx, len;int 0x80;ret;
main.asm lib.asm
20
External Library Example
main: main.o lib.old –o main main.o lib.o
main.o: main.asmnasm –f elf –g –F stabs
main.asmlib.o: lib.asm
nasm –f elf –g –F stabs lib.asm
Makefile
21
External Library Example
UNIX> makenasm –f elf –g –F stabs main.asmnasm –f elf –g –F stabs lib.asmld –o main main.o lib.o
UNIX> ./mainERROR!!
UNIX>
link the two together
22
External Library Example
• You can also access data from external libraries
• Just need to define data as GLOBAL or EXTERN• EXTERN: data gets “imported” from somewhere else • GLOBAL: data gets “exported” to somewhere else
23
External Library Example
section .textglobal _startextern msgA, msgB, msgC, len;
_start:mov eax, msgA;call my_print;mov eax, msgB;call my_print
; my print not shown; clean exit not shown
section .dataglobal msgA, msgB, len;
msgA: db “AAAAA”,10;msgB: db “BBBBB” , 10;len: equ $-msgB;
main.asm lib.asm
24
External Library Example
UNIX> makenasm –f elf –g –F stabs main.asmnasm –f elf –g –F stabs lib.asmld –o main main.o lib.o
UNIX> ./mainAAAAABBBBBUNIX>
25
External Libraries
• “Think big / long term”
• Create useful libraries
• Be wary of “dead” procedures• Procedures in object file but never used• Wastes memory: entire object file linked into executable• Important for embedded systems: memory isn’t cheap
26
External Libraries
• User comment headers!
; name of procedure; summary of procedure functionality ; expected input argument(s) (and registers); expected return value(s) (and registers); information about data that gets modified; example usage
27
Outline
• Local Labels• Jump Lengths• External Libraries• Macros
28
Macros
• Alternative to a procedure• Assembler literally replaces macro “call” with code
• “Expanding the macro”• Similar to a #include in C/C++• Similar to a copy / paste
• (Original file not modified)• Code put it memory
• Don’t actually “call” a macro• Macro does not “return”
29
Macro Definition
%macro <name> <number of arguments>
<instruction><instruction><instruction>
%endmacro macro can be defined anywhere in code file
30
Macro Example
%macro writeMsg 2
mov eax, 4;mov ebx, 1;mov ecx, %1;mov edx; %2;int 0x80;
%endmacro
writeMsg msg, len;
define a macro called writeMsg that takes two arguments
31
Macro Example
%macro writeMsg 2
mov eax, 4;mov ebx, 1;mov ecx, %1;mov edx; %2;int 0x80;
%endmacro
writeMsg msg, len;
put argument #1 into ecx
access arguments with %<num>
32
Macro Example
%macro writeMsg 2
mov eax, 4;mov ebx, 1;mov ecx, %1;mov edx; %2;int 0x80;
%endmacro
writeMsg msg, len;
put argument #2 into edx
access arguments with %<num>
33
Macro Example
%macro writeMsg 2
mov eax, 4;mov ebx, 1;mov ecx, %1;mov edx; %2;int 0x80;
%endmacro
writeMsg msg, len;
Argument references always start at 1, not 0
34
Macro Example
%macro writeMsg 2
mov eax, 4;mov ebx, 1;mov ecx, %1;mov edx; %2;int 0x80;
%endmacro
writeMsg msg, len;
end of macro definition
35
Macro Example
%macro writeMsg 2
mov eax, 4;mov ebx, 1;mov ecx, %1;mov edx; %2;int 0x80;
%endmacro
writeMsg msg, len;
instruction (in .text) to expand (call) the writeMsg macro with two arguments: arg #1: msgarg #2: len
comma(s) separate arguments
(assume msg and len are declared already in .data).
36
Macro Definition
• How to declare macro that takes no arguments? • Declare zero arguments
%macro name 0
<instruction><instruction>
%endmacro
37
Macros
• Be careful using global label(s) in macros• Why?
• In macro, define local labels using %%<label>
%macro myExit 0jmp %%_exit ; jump to local label %%_exitmov eax,4 ; write sys call (ever executed?)mov ebx,1 ; write to stdoutmov ecx,msg ; msg declared in .data (not shown)mov edx, len ; len declared in .data (not shown)int 0x80 ; make system call
%%_exit: ; local label in macromov eax, 1; ; exit sys callmov ebx, 0; ; exit 0int 0x80 ; make sys call
%endmacro
my_exit ; in .text
39
Macro Example
UNIX> ./a.outUNIX> no output
jumped past sys_write call to local label %%_exit: in macro
%macro myExit 0;;; jmp %%_exit ; jump to local label COMMENTED
OUTmov eax,4 ; write sys call (ever executed?)mov ebx,1 ; write to stdoutmov ecx,msg ; msg declared in .data (not shown)mov edx, len ; len declared in .data (not shown)int 0x80 ; make system call
%%_exit: ; local label in macromov eax, 1; ; exit sys callmov ebx, 0; ; exit 0int 0x80 ; make sys call
%endmacro
my_exit ; in .text
41
Macro Example
UNIX> ./a.outthis is a message!!UNIX>
did not jump past sys_write call to local label %%_exit in macros
42
Macro Library
• Can define macro(s) in external files• You do NOT assemble / link macro library• Must use %include directive
• Put directive at top of .text section
%include <filename>
43
Macro Library
%macro writeMsg 2
mov eax,4;mov ebx,1;mov ecx, %1;mov edx, %2;int 0x80;
%endmacro
section .datamsg: db “message!!”,10len: equ $-msg;
section .text%include “lib.mac”global _start:_start:
writeMsg msg,len
;sys_exit call not shownmain.asm lib.mac
44
Macro Library
• Again, no need to assemble macro library nor link with main.o
UNIX> makenasm –f elf –g –F stabs main.asmld –o main main.oUNIX> ./mainmessage!!UNIX>
45
Macros vs. Procedures
• Macros are faster than procedures• No call or ret• No need to allocate a stack frame for the procedure
• Macros require extra memory• Each time macro “called”, lines of code duplicated in memory
• Macros are harder to debug than procedures• Cannot “step through” a macro