Transcript
Page 1: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

Assembly 08

Page 2: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

2

Outline

• Local Labels• Jump Lengths• External Libraries• Macros

Page 3: Assembly 08. 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

Page 4: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 5: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 6: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

6

Local Labels

• Local Labels are extremely useful for labels in procedures• E.g.,

function_1:.loop:

function_2:.loop:

Page 7: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

7

Outline

• Local Labels• Jump Lengths• External Libraries• Macros

Page 8: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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)

Page 9: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

9

Jump Lengths

• Three types of jumps in x86:

• Short Jump• Near Jump• Far Jump

• Note: “far jumps” are extremely rare

Page 10: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 11: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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;

Page 12: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

12

Far Jump

• Jump target is beyond code segment• Extremely rare• (Will not use in this class)

code segment

other code

segment

far jump

Page 13: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 14: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

14

Outline

• Local Labels• Jump Lengths• External Libraries• Macros

Page 15: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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.

Page 16: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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++

Page 17: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

assembly source code

file(s)

External Libraries

.asm

.asm

.asm

assembler

.o

.o

.o

linker

executable

object file(s)

executable

program file

Page 18: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 19: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 20: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 21: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 22: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 23: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 24: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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>

Page 25: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 26: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 27: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

27

Outline

• Local Labels• Jump Lengths• External Libraries• Macros

Page 28: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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”

Page 29: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

29

Macro Definition

%macro <name> <number of arguments>

<instruction><instruction><instruction>

%endmacro macro can be defined anywhere in code file

Page 30: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 31: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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>

Page 32: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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>

Page 33: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 34: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 35: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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).

Page 36: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

36

Macro Definition

• How to declare macro that takes no arguments? • Declare zero arguments

%macro name 0

<instruction><instruction>

%endmacro

Page 37: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

37

Macros

• Be careful using global label(s) in macros• Why?

• In macro, define local labels using %%<label>

Page 38: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

%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

Page 39: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

39

Macro Example

UNIX> ./a.outUNIX> no output

jumped past sys_write call to local label %%_exit: in macro

Page 40: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

%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

Page 41: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

41

Macro Example

UNIX> ./a.outthis is a message!!UNIX>

did not jump past sys_write call to local label %%_exit in macros

Page 42: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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>

Page 43: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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

Page 44: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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>

Page 45: Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1

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


Top Related