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

Slides:



Advertisements
Similar presentations
Hello World!. PC / MS-DOS code segment para assume cs:code,ds:code org 0100h start: mov dx,offset message ;point to message mov ah,09h ;func# to printstring.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Assembler/Linker/Loader Mooly Sagiv html:// Chapter 4.3 J. Levine: Linkers & Loaders
1 Starting a Program The 4 stages that take a C++ program (or any high-level programming language) and execute it in internal memory are: Compiler - C++
Chapter 8: Programming the Microprocessor. Copyright ©2009 by Pearson Education, Inc. Upper Saddle River, New Jersey All rights reserved. The Intel.
Practical Session 3. The Stack The stack is an area in memory that its purpose is to provide a space for temporary storage of addresses and data items.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
Position Independent Code self sufficiency of combining program.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
Guide To UNIX Using Linux Third Edition
Practical Session 8 Computer Architecture and Assembly Language.
ICS312 Set 11 Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External.
Languages and tools. BY SA machine code.
Introduction to Subroutines. All the combinations in which a subroutine can be written 1. The subroutine may be: a. Internal or b. External 2. The type.
Chapter 3 Elements of Assembly Language. 3.1 Assembly Language Statements.
Today’s topics Parameter passing on the system stack Parameter passing on the system stack Register indirect and base-indexed addressing modes Register.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Introduction to C Programming CE Lecture 7 Compiler options and makefiles.
Objective At the conclusion of this chapter you will be able to:
The x86 PC Assembly Language, Design, and Interfacing By Muhammad Ali Mazidi, Janice Gillespie Mazidi and Danny Causey © 2010, 2003, 2000, 1998 Pearson.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
ICS312 Set 14 MACROS. Macros The program structure is similar to that for procedures. As for procedure names, macro names represent a group of instructions.
Lecture 7 A closer look at procedures Dr. Dimitrios S. Nikolopoulos CSL/UIUC.
CNIT 127: Exploit Development Ch 3: Shellcode. Topics Protection rings Syscalls Shellcode nasm Assembler ld GNU Linker objdump to see contents of object.
Assembly Language for x86 Processors 7th Edition Chapter 13: High-Level Language Interface (c) Pearson Education, All rights reserved. You may modify.
Microprocessors The ia32 User Instruction Set Jan 31st, 2002.
ECE291 Lecture 6 Procedures and macros. ECE 291 Lecture 6Page 2 of 36 Lecture outline Procedures Procedure call mechanism Passing parameters Local variable.
CS412/413 Introduction to Compilers and Translators April 14, 1999 Lecture 29: Linking and loading.
Assembly 03. Outline inc, dec movsx jmp, jnz Assembly Code Sections Labels String Variables equ $ Token 1.
Assembly 07. Outline Boxes within Boxes Procedure Definition call, ret Saving / Restoring Registers Argument(s) Return Value(s) Global vs. Local Data.
Practical Session 5 Computer Architecture and Assembly Language.
1 The Stack and Procedures Chapter 5. 2 A Process in Virtual Memory  This is how a process is placed into its virtual addressable space  The code is.
Assembly 06. Outline cmp (review) Jump commands test mnemonic bt mnemonic Addressing 1.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Assembly 09. Outline Strings in x86 esi, edi, ecx, eax stosb, stosw, stosd cld, std rep loop 1.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Practical Session 8. Position Independent Code- self sufficiency of combining program Position Independent Code (PIC) program has everything it needs.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Practical Session 4. GNU Linker Links object files together Used as the last step in the compilation We will use ld to link together compiled assembly.
1 Building a program in C: Preprocessor, Compilation and Linkage.
Program Execution and ELF Files Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014 Abed Asi.
Practical Session 6 Computer Architecture and Assembly Language.
Practical Session 3.
Lecture 3 Translation.
Computer Architecture and Assembly Language
Practical Session 5.
Chapter 8: Programming the Microprocessor
Assembly Language Lab 9.
Format of Assembly language
MODULAR PROGRAMMING Many programs are too large to be developed by one person. programs are routinely developed by teams of programmers The linker program.
Assembly Language programming
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Additional Assembly Programming Concepts
Computer Architecture and Assembly Language
Microprocessor and Assembly Language
High-Level Language Interface
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and Assembly Language
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Procedures & Macros Introduction Syntax Difference.
Presentation transcript:

Assembly 08

Outline Local Labels Jump Lengths External Libraries Macros 1

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

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

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

Local Labels Local Labels are extremely useful for labels in procedures E.g., function_1:.loop: function_2:.loop: 5

Outline Local Labels Jump Lengths External Libraries Macros 6

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

Jump Lengths Three types of jumps in x86: Short Jump Near Jump Far Jump Note: “far jumps” are extremely rare 8

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

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

Far Jump Jump target is beyond code segment Extremely rare (Will not use in this class) 11 code segment other code segment far jump

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

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

assembly source code file(s) External Libraries.asm assembler.o linker executable object file(s) 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 “imported” from somewhere else GLOBAL -> procedure gets “exported” to 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 _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

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

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 “imported” from somewhere else GLOBAL: data gets “exported” to somewhere else 21

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

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 %endmacro 28 macro can be defined anywhere in code file

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 %

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 %

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 %endmacro 35

Macros Be careful using global label(s) in macros Why? In macro, define local labels using % 36

%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

Macro Example UNIX>./a.out UNIX> 38 no output jumped past sys_write call to local label %_exit: in macro

%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

Macro Example UNIX>./a.out this is a message!! UNIX> 40 did not jump past sys_write call to local label %_exit in macros

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

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

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