Assembly 08

advertisement

Assembly 08

1

Outline

• Local Labels

• Jump Lengths

• External Libraries

• Macros

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

2

Local labels may exist within a procedure

3

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

4

Local Labels

local labels belong to previous (above) global label

local labels cannot be referenced before the global label that

“owns” it

A:

.loop:

.test:

B:

.loop

.test:

5

Local Labels

Local Labels are extremely useful for labels in procedures

• E.g., function_1:

.loop: function_2:

.loop:

6

Outline

• Local Labels

• Jump Lengths

• External Libraries

• Macros

7

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)

8

Jump Lengths

Three types of jumps in x86:

Short Jump

Near Jump

Far Jump

• Note: “far jumps” are extremely rare

Short Jump

9

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

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;

10

Far Jump

Jump target is beyond code segment

• Extremely rare

• (Will not use in this class) code segment far jump other code segment

11

Jump Lengths

• So, if you encounter “error: short jump is out of range”, use the near keyword:

; If this produces an

error,

je _loop je near _loop ; use near keyword

12

Outline

• Local Labels

• Jump Lengths

• External Libraries

• Macros

13

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

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

External Libraries

.asm

.o

executable

.o

.asm

.asm

assembly source code file(s) assembler

.o

object file(s) linker executable program file

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 “importedfrom somewhere else

GLOBAL -> procedure gets “exportedto somewhere else

17

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 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; lib.asm

18 main.asm

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

Makefile

19

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

External Library Example

• You can also access data from external libraries

• Just need to define data as GLOBAL or EXTERN

EXTERN: data gets “importedfrom somewhere else

GLOBAL: data gets “exportedto somewhere else

21

External Library Example 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 main.asm

22 section .data

global msgA, msgB, len; msgA: db “AAAAA”,10; msgB: db “BBBBB” , 10; len: equ $-msgB; lib.asm

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

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

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

Outline

• Local Labels

• Jump Lengths

• External Libraries

• Macros

26

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

Macro Definition

%macro <name> <number of arguments>

<instruction>

<instruction>

<instruction>

%endmacro macro can be defined anywhere in code file

28

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

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

%<num>

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

%<num>

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

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

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

Macro Definition

• How to declare macro that takes no arguments?

• Declare zero arguments

%macro name 0

<instruction>

<instruction>

%endmacro

35

Macros

• Be careful using global label(s) in macros

• Why?

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

36

%macro myExit 0 jmp %%_exit mov eax,4 mov ebx,1 mov ecx,msg mov edx, len int 0x80

%%_exit: mov eax, 1; mov ebx, 0; int 0x80

%endmacro my_exit

; jump to local label %%_exit

; write sys call (ever executed?)

; write to stdout

; msg declared in .data (not shown)

; len declared in .data (not shown)

; make system call

; local label in macro

; exit sys call

; exit 0

; make sys call

; in .text

Macro Example

UNIX> ./a.out

UNIX> no output jumped past sys_write call to local label %%_exit: in macro

38

%macro myExit 0

;;; jmp %%_exit

OUT mov eax,4 mov ebx,1 mov ecx,msg mov edx, len int 0x80

%%_exit: mov eax, 1; mov ebx, 0; int 0x80

%endmacro my_exit

; jump to local label COMMENTED

; write sys call (ever executed?)

; write to stdout

; msg declared in .data (not shown)

; len declared in .data (not shown)

; make system call

; local label in macro

; exit sys call

; exit 0

; make sys call

; in .text

Macro Example

UNIX> ./a.out

this is a message!!

UNIX> did not jump past sys_write call to local label

%%_exit in macros

40

41

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>

Macro Library 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.asm

42

%macro writeMsg 2 mov eax,4; mov ebx,1; mov ecx, %1; mov edx, %2; int 0x80;

%endmacro lib.mac

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

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

Download