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.

Slides:



Advertisements
Similar presentations
Copyright © 2000, Daniel W. Lewis. All Rights Reserved. CHAPTER 5 MIXING C AND ASSEMBLY.
Advertisements

Chapter 7 Process Environment Chien-Chung Shen CIS, UD
C Programming and Assembly Language Janakiraman V – NITK Surathkal 2 nd August 2014.
Practical Session 3 Computer Architecture and Assembly Language.
Practical session 7 review. Little – Endian What’s in memory? Section.rodata a: DB ‘hello’, 0x20, ’world’, 10, 0 b: DW ‘hello’, 0x20, ’world’, 10, 0 c:
Practical Session 5. Addressing Modes #include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE],
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
Practical Session 5. Addressing Modes #include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE],
Practical Session 9 Co-Routines. Co-Routines Co-routine state.
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.
Accessing parameters from the stack and calling functions.
Practical Session 5. Addressing Modes #include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE],
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.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Position Independent Code self sufficiency of combining program.
CS2422 Assembly Language & System Programming October 24, 2006.
Practical Session 8 Computer Architecture and Assembly Language.
Universal Concepts of Programming Creating and Initializing local variables on the stack Variable Scope and Lifetime Stack Parameters Stack Frames Passing.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
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, _,
Lecture-1 Compilation process
Assembly Language for Intel-Based Computers, 6 th Edition Chapter 8: Advanced Procedures (c) Pearson Education, All rights reserved. You may.
Practical Session 7 Computer Architecture and Assembly Language.
1 Carnegie Mellon Recitation : Introduction to Computer Systems Recitation 15: December 3, 2012 Daniel Sedra Section C.
Exploitation Of Windows Buffer Overflows. What is a Buffer Overflow A buffer overflow is when memory is copied to a location that is outside of its allocated.
Computer Architecture and Assembly Language. Byte structure : a byte has 8 bits MSB (Most Significant Bit) LSB (Least Significant Bit) Data Representation.
Practical Session 5. Addressing Modes #include #define VECTOR_SIZE 5 #define MATRIX_ROWS 2 #define MATRIX_COLUMNS 3 extern int DotProduct(int V1[VECTOR_SIZE],
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 22: Unconventional.
Sahar Mosleh California State University San MarcosPage 1 Stack operations, Applications and defining procedures.
Practical Session 4 Computer Architecture and Assembly Language.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
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.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Practical Session 7. Co-Routines co-routines are assembly implementation of threads each co-routine decides to which co- routine to pass a control - unlike.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
CS429 Computer Architecture Topics Simple C program Basic structure, functions, separate files Compilation Phases, options Assembler GNU style, byte ordering,
Computer Architecture and Assembly Language
1 Linking. 2 Outline What is linking and why linking Complier driver Static linking Symbols & Symbol Table Suggested reading: 7.1~7.5.
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.
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.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Chapter 7 Process Environment Chien-Chung Shen CIS/UD
Practical Session 5 Computer Architecture and Assembly Language.
Practical Session 3 Computer Architecture and Assembly Language.
Practical Session 3.
Computer Architecture and Assembly Language
Practical Session 5.
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Homework Reading Machine Projects Labs PAL, pp ,
Computer Architecture and Assembly Language
Exploiting & Defense Day 2 Recap
Introduction to Compilers Tim Teitelbaum
Computer Architecture and Assembly Language
Assembly Language Programming II: C Compiler Calling Sequences
Practical Session 4.
Multi-modules programming
X86 Assembly Review.
ICS51 Introductory Computer Organization
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
Presentation transcript:

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 without using c. Usage:  nasm –f elf myprog.s –o myprog.o  ld -m elf_i386 myprog.o –o myprog.bin

Command Line Arguments in NASM

Command-line arguments In Linux, we receive command-line arguments on the stack as execution starts: - The first argument; number of arguments (i.e. argc) - The rest of the arguments; each one is a pointer to an argument string. (i.e. argv[0], argv[1] argv[2])

Function calls in NASM

Caller side The caller pushes the function's parameters on the stack, one after another, in reverse order. The caller executes a CALL instruction to pass control to the callee. This CALL is either near or far depending on the memory model. The address of the next instruction is pushed onto the stack as the return address.

Stack structure EBP Ret add (offset) Param #1 … Param #2 dword argc argv[0] … argv[1] argv[2] Function_start

Function call example STR: DB ‘ Printing two ints: %d, %d ’,10,0 … MOV EAX, 15 PUSH EAX ; 3 rd parameter PUSH DWORD 20 ; 2 nd parameter PUSH DWORD STR ; 1 st parameter CALL PRINTF ADD ESP, 12 ; 3 dwords = 12 bytes

Stack contents EBP Ret add (offset) STR 20 dword ; how to get parameters PUSH EBP MOV EBP, ESP MOV EAX, dword [EBP+8] ; 1 st parameter MOV EBX, dword [EBP+12] ; 2 nd parameter MOV ECX, dword [EBP+16] ; 3 rd parameter … 15

section.rodata error_string: db"Not enough arguments!",10,0 section.bss input_string_ptr: resd1; Will contain the pointer to the input string output_string: resb256; Will contain the actual string after copy section.text global _start _start: pop ebx; Contains the number of arguments (Including argv[0]) cmpebx, 2 jlprint_error popebx; First argument: Name of the program popebx; The first argument - Our argument movdword [input_string_ptr], ebx pushdword [input_string_ptr] pushdword output_string callcpy_string; Copy the string to our own buffer addesp, 8 Print String

pushdword output_string callinsert_linefeed; Add a linefeed character (not present in the input) addesp, 4 pushdword output_string callmy_print; Print the output string addesp, 4 mov ebx,0; Exit with return code of 0 (no error) proc_exit: mov eax,1 ; The system call for exit (sys_exit) int 80h print_error:; This is not a function! Just a jmp destination! pusherror_string callmy_print addesp, 4 movebx, 1; Exit with return code of 1 (error) jmpproc_exit Print String

section.rodata error_string: db"Not enough arguments!",10,0 section.bss input_string_ptr: resd1; Will contain the pointer to the input string output_string: resb256; Will contain the actual string after copy section.text global _start _start: pop ebx; Contains the number of arguments (Including argv[0]) cmpebx, 2 jlprint_error popebx; First argument: Name of the program popebx; The first argument - Our argument movdword [input_string_ptr], ebx pushdword [input_string_ptr] pushdword output_string callcpy_string; Copy the string to our own buffer addesp, 8 pushdword output_string callinsert_linefeed; Add a linefeed character (not present in the input) addesp, 4 pushdword output_string callmy_print; Print the output string addesp, 4 mov ebx,0; Exit with return code of 0 (no error) proc_exit: mov eax,1 ; The system call for exit (sys_exit) int 80h print_error:; This is not a function! Just a jmp destination! pusherror_string callmy_print addesp, 4 movebx, 1; Exit with return code of 1 (error) jmpproc_exit Print String

;;;;; This function calculates the length of a string ;;;;; Parameter 1: A pointer to a string ;;;;; Return Value: Integer calc_str_len: pushebp movebp, esp pushebx pushecx movebx, dword [ebp+8]; The input in ebx moveax, 0; The result will be in eax len_next_char: movcl, byte [ebx] cmpcl, 0 jelen_next_char_end inceax incebx jmplen_next_char len_next_char_end: pop ecx popebx movesp, ebp popebp ret calc_str_len

;;;;; This function prints an input string. Note: input string must end with 0! ;;;;; Parameter 1: A pointer to a string ;;;;; Return Value: VOID my_print: pushebp movebp, esp pushad movecx, dword [ebp+8] pushecx callcalc_str_len; Calculate the length of the input string addesp, 4 movedx, eax mov eax, 4 mov ebx, 1 int 80h popad movesp, ebp popebp ret my_print

;;;;; This function will copy a string from an input memory buffer to another ;;;;; Parameter 1: Destination Buffer (Address) ;;;;; Parameter 2: Source Buffer (Address) ;;;;; Return Value: VOID cpy_string: pushebp movebp, esp pushad movebx, dword [ebp+8]; Pointer to Destination movecx, dword [ebp+12]; Pointer to Source movdl, byte[ecx] cpy_next_char: movbyte [ebx], dl cmpdl, 0 jecpy_next_char_end incebx incecx movdl, byte[ecx] jmp cpy_next_char cpy_next_char_end: popad movesp, ebp popebp ret cpy_string

;;;;; This function will add a linefeed character to a string in a memory buffer ;;;;; Parameter 1: Address of memory buffer containing the string ;;;;; Return Value: VOID insert_linefeed: pushebp movebp, esp pushad movebx, dword [ebp+8] pushebx call calc_str_len addesp,4 addebx, eax; Go to the end of the string movbyte [ebx], 10; Add linefeed incebx movbyte [ebx], 0; Add null terminal popad movesp, ebp popebp ret insert_linefeed

ld vs. gcc argc argv[0] … argv[1] argv[2] _start argc &{argv[0],arg[1],…} … main Ret address This is just like C’s main(int argc, char* argv[])