Download presentation
Presentation is loading. Please wait.
Published byMoses Walton Modified over 8 years ago
1
Assembly 08
2
Outline Local Labels Jump Lengths External Libraries Macros 1
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 2
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 3
5
Local Labels 4 A:.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: 5
7
Outline Local Labels Jump Lengths External Libraries Macros 6
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) 7
9
Jump Lengths Three types of jumps in x86: Short Jump Near Jump Far Jump Note: “far jumps” are extremely rare 8
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: je _loop 9
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: near ; je near loop; 10
12
Far Jump Jump target is beyond code segment Extremely rare (Will not use in this class) 11 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 12
14
Outline Local Labels Jump Lengths External Libraries Macros 13
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. 14
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++ 15
17
assembly source code file(s) External Libraries.asm assembler.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 17
19
External Library Example section.text global _start extern print_err; _start: call print_err; mov eax, 1; mov ebx, 0; int 0x80; section.text global _start extern print_err; _start: call print_err; mov eax, 1; mov ebx, 0; int 0x80; 18 section.text global 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; section.text global 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.asmlib.asm
20
External Library Example main: main.o lib.o ld –o main main.o lib.o main.o: main.asm nasm –f elf –g –F stabs main.asm lib.o: lib.asm nasm –f elf –g –F stabs lib.asm 19 Makefile
21
External Library Example UNIX> make nasm –f elf –g –F stabs main.asm nasm –f elf –g –F stabs lib.asm ld –o main main.o lib.o UNIX>./main ERROR!! UNIX> 20 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 21
23
External Library Example 22 section.text global _start extern 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.text global _start extern 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.data global msgA, msgB, len; msgA: db “AAAAA”,10; msgB: db “BBBBB”, 10; len: equ $-msgB; section.data global msgA, msgB, len; msgA: db “AAAAA”,10; msgB: db “BBBBB”, 10; len: equ $-msgB; main.asmlib.asm
24
External Library Example UNIX> make nasm –f elf –g –F stabs main.asm nasm –f elf –g –F stabs lib.asm ld –o main main.o lib.o UNIX>./main AAAAA BBBBB UNIX> 23
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 24
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 25
27
Outline Local Labels Jump Lengths External Libraries Macros 26
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” 27
29
Macro Definition %macro %endmacro 28 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; 29 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; 30 put argument #1 into ecx access arguments with %
32
Macro Example %macro writeMsg 2 mov eax, 4; mov ebx, 1; mov ecx, %1; mov edx; %2; int 0x80; %endmacro writeMsg msg, len; 31 put argument #2 into edx access arguments with %
33
Macro Example %macro writeMsg 2 mov eax, 4; mov ebx, 1; mov ecx, %1; mov edx; %2; int 0x80; %endmacro writeMsg msg, len; 32 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; 33 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; 34 instruction (in.text) to expand (call) the writeMsg macro with two arguments: arg #1: msg arg #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 %endmacro 35
37
Macros Be careful using global label(s) in macros Why? In macro, define local labels using % 36
38
%macro myExit 0 jmp %_exit; jump to local label %_exit mov eax,4; write sys call (ever executed?) mov ebx,1; write to stdout mov 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 macro mov eax, 1;; exit sys call mov ebx, 0;; exit 0 int 0x80; make sys call %endmacro my_exit; in.text
39
Macro Example UNIX>./a.out UNIX> 38 no output jumped past sys_write call to local label %_exit: in macro
40
%macro myExit 0 ;;; jmp %_exit; jump to local label COMMENTED OUT mov eax,4; write sys call (ever executed?) mov ebx,1; write to stdout mov 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 macro mov eax, 1;; exit sys call mov ebx, 0;; exit 0 int 0x80; make sys call %endmacro my_exit; in.text
41
Macro Example UNIX>./a.out this is a message!! UNIX> 40 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 41
43
Macro Library %macro writeMsg 2 mov eax,4; mov ebx,1; mov ecx, %1; mov edx, %2; int 0x80; %endmacro %macro writeMsg 2 mov eax,4; mov ebx,1; mov ecx, %1; mov edx, %2; int 0x80; %endmacro 42 section.data msg: db “message!!”,10 len: equ $-msg; section.text %include “lib.mac” global _start: _start: writeMsg msg,len ;sys_exit call not shown section.data msg: db “message!!”,10 len: equ $-msg; section.text %include “lib.mac” global _start: _start: writeMsg msg,len ;sys_exit call not shown main.asmlib.mac
44
Macro Library Again, no need to assemble macro library nor link with main.o UNIX> make nasm –f elf –g –F stabs main.asm ld –o main main.o UNIX>./main message!! UNIX> 43
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 44
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.