Computer Architecture and Assembly Language

Slides:



Advertisements
Similar presentations
Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
Advertisements

David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 Lecture 5: Procedures Assembly Language for Intel-Based Computers, 4th edition Kip R. Irvine.
Accessing parameters from the stack and calling functions.
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.
Practical Session 8 Computer Architecture and Assembly Language.
CEG 320/520: Computer Organization and Assembly Language ProgrammingIntel Assembly 1 Intel IA-32 vs Motorola
6.828: PC hardware and x86 Frans Kaashoek
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 7 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
1 Carnegie Mellon Stacks : Introduction to Computer Systems Recitation 5: September 24, 2012 Joon-Sup Han Section F.
Practical Session 4. Labels Definition - advanced label: (pseudo) instruction operands ; comment valid characters in labels are: letters, numbers, _,
Lecture-1 Compilation process
Introduction to Information Security מרצים : Dr. Eran Tromer: Prof. Avishai Wool: מתרגלים : Itamar Gilad
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Today’s topics Procedures Procedures Passing values to/from procedures Passing values to/from procedures Saving registers Saving registers Documenting.
ELF binary # readelf -a foo.out ELF Header:
Practical Session 4 Computer Architecture and Assembly Language.
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.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Assembly 08. Outline Local Labels Jump Lengths External Libraries Macros 1.
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
CSC 221 Computer Organization and Assembly Language Lecture 16: Procedures.
Practical Session 8. Position Independent Code- self sufficiency of combining program Position Independent Code (PIC) program has everything it needs.
Linking I Topics Assembly and symbol resolution Static linking Systems I.
ICS51 Introductory Computer Organization Accessing parameters from the stack and calling functions.
Practical Session 6 Computer Architecture and Assembly Language.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Practical Session 3.
Section 5: Procedures & Stacks
Practical Session 5.
Instruction Set Architecture
Assemblers, linkers, loaders
Computer Architecture and Assembly Language
Homework / Exam Return and Review Exam #1 Reading Machine Projects
Introduction to Information Security
Assembly language.
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
Homework In-line Assembly Code Machine Language
Computer Architecture
High-Level Language Interface
asum.ys A Y86 Programming Example
Computer Architecture and Assembly Language
Computer Architecture and Assembly Language
Assembly Language Programming I: Introduction
Assembly Language Programming II: C Compiler Calling Sequences
Computer Architecture and Assembly Language
Practical Session 4.
EECE.3170 Microprocessor Systems Design I
The Runtime Environment
EECE.3170 Microprocessor Systems Design I
Machine-Level Programming: Introduction
Multi-modules programming
Computer Architecture and System Programming Laboratory
Computer Organization and Assembly Language
Program Assembly.
ICS51 Introductory Computer Organization
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
Computer Architecture and System Programming Laboratory
Presentation transcript:

Computer Architecture and Assembly Language Practical Session 8

Position Independent Code - PIC PIC has everything it needs internally PIC can be placed somewhere in memory, is executed properly regardless of its absolute address PIC can be added to any other program, without fear that it might not work

Position Independent Code - requirements No direct usage of labels No library functions ? One section only ‘jump’ and ‘call’ functions ?   only system calls   they are relative

No library functions  only system calls We don’t know if and where the library functions are. Thus there are no “printf” “gets” and so on. To perform I/O operation we have to use the Linux system calls because INT 0x80 isn’t a regular procedure - it is called via the interrupt table which is static.

One section only We put all the code in a single section – .text (read-only) or .data (read-write). Both .text and .data sections may contain any valid assembly instruction. Usage of a single section gives us a possibility to calculate a relative offset between a pair of code instruction addresses, and thus use offset instead of absolute address.

Only relative jumps If all the code changes its position, relative jumps are still valid, because the address difference is preserved.  Address of myFunc label = 0x1F  Address of the next instruction after the call (i.e. ‘mov [answer], eax’) is 0xA  0x1F-0xA=0x15, and we get exactly the binary code written here ‘E815000000’

No direct usage of labels Labels are resolved at compile time to absolute address An absolute addresses of name would be resolved based on the offset of name in .rodata section. If the code is moved, the absolute address that was calculated before the moving would not be correct any more

Using labels – PIC example section .text name: db "Hello",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 stack ESP

address of ‘sub edx, next_i-name’ Using labels – PIC example section .text name: db "Hello",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 stack address of ‘sub edx, next_i-name’ ESP

address of ‘sub edx, next_i-name’ Using labels – PIC example section .text name: db "Hello",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 stack address of ‘sub edx, next_i-name’ address of ‘next_i’ ESP

address of ‘sub edx, next_i-name’ Using labels – PIC example section .text name: db "Hello",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ; edx gets address of ‘next_i’ ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 stack address of ‘sub edx, next_i-name’ ESP

Using labels – PIC example section .text name: db "Hello",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret ; EIP gets address of ‘sub edx, next_i-name’ _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 stack ESP

Using labels – PIC example section .text name: db "Hello",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name ; edx = ‘next_i’ address – (‘next_i’ address – ‘name’ address) mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 the address difference between “next_i” and “name” is constant even if the code changes it’s position

Using labels – PIC example section .text name: db "Hello",10,0 nameLen equ $ - name global _start get_my_loc: call next_i next_i: pop edx ret _start: call get_my_loc sub edx, next_i – name mov ecx, edx mov edx, nameLen mov eax, 4 mov ebx, 1 int 80h mov eax, 1 why we may use ‘nameLen’ label directly ?

Using labels – PIC example >nasm -f elf sample.s -l sample.lst 0x0C = ‘next_i’ – ‘name’