ECE 498 Linux Assembly Language Lecture 1

Similar documents
ECE 471 Embedded Systems Lecture 4

ECE 471 Embedded Systems Lecture 5

ECE 598 Advanced Operating Systems Lecture 10

ECE 471 Embedded Systems Lecture 6

ECE 598 Advanced Operating Systems Lecture 2

ECE 471 Embedded Systems Lecture 4

ECE 486/586. Computer Architecture. Lecture # 7

ECE 571 Advanced Microprocessor-Based Design Lecture 13

ECE 598 Advanced Operating Systems Lecture 11

ECE 571 Advanced Microprocessor-Based Design Lecture 3

Computer Systems Architecture I. CSE 560M Lecture 3 Prof. Patrick Crowley

Systems Architecture I

ECE 571 Advanced Microprocessor-Based Design Lecture 4

CS 61C: Great Ideas in Computer Architecture. (Brief) Review Lecture

ECE 598 Advanced Operating Systems Lecture 10

Lecture 4: Instruction Set Architecture

OS lpr. www. nfsd gcc emacs ls 1/27/09. Process Management. CS 537 Lecture 3: Processes. Example OS in operation. Why Processes? Simplicity + Speed

ECE 598 Advanced Operating Systems Lecture 14

Computer Systems Laboratory Sungkyunkwan University

55:132/22C:160, HPCA Spring 2011

Lec 13: Linking and Memory. Kavita Bala CS 3410, Fall 2008 Computer Science Cornell University. Announcements

Architectures & instruction sets R_B_T_C_. von Neumann architecture. Computer architecture taxonomy. Assembly language.

ECE 571 Advanced Microprocessor-Based Design Lecture 12

ECE 598 Advanced Operating Systems Lecture 14

CS 61C: Great Ideas in Computer Architecture CALL continued ( Linking and Loading)

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

ECE 471 Embedded Systems Lecture 6

Assembly Language. Lecture 2 - x86 Processor Architecture. Ahmed Sallam

Instructions: Language of the Computer

Instruction Sets Ch 9-10

Instruction Sets Ch 9-10

ECE 471 Embedded Systems Lecture 8

www nfsd emacs lpr Process Management CS 537 Lecture 4: Processes Example OS in operation Why Processes? Simplicity + Speed

x86 assembly CS449 Fall 2017

Overview of the MIPS Architecture: Part I. CS 161: Lecture 0 1/24/17

Instruction Set. Instruction Sets Ch Instruction Representation. Machine Instruction. Instruction Set Design (5) Operation types

Process Environment. Pradipta De

ISA and RISCV. CASS 2018 Lavanya Ramapantulu

Protection and System Calls. Otto J. Anshus

COSC 6385 Computer Architecture. Instruction Set Architectures

Advanced Computer Architecture

COS 318: Operating Systems

COE608: Computer Organization and Architecture

Assembly Language. Lecture 2 x86 Processor Architecture

Virtual Machines and Dynamic Translation: Implementing ISAs in Software

COS 318: Operating Systems. Overview. Andy Bavier Computer Science Department Princeton University

EC 413 Computer Organization

ECE 598 Advanced Operating Systems Lecture 12

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

Porting Linux to x86-64

Lecture 4: Instruction Set Design/Pipelining

Process Address Spaces and Binary Formats

Page 1. Structure of von Nuemann machine. Instruction Set - the type of Instructions

Chapter 2A Instructions: Language of the Computer

CNIT 127: Exploit Development. Ch 3: Shellcode. Updated

Lecture 7: Examples, MARS, Arithmetic

Machine Language, Assemblers and Linkers"

LECTURE 2: INSTRUCTIONS

CSCI 402: Computer Architectures. Instructions: Language of the Computer (4) Fengguang Song Department of Computer & Information Science IUPUI

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

MIPS (SPIM) Assembler Syntax

Language Translation. Compilation vs. interpretation. Compilation diagram. Step 1: compile. Step 2: run. compiler. Compiled program. program.

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

Administrivia. Lab 1 due Friday 12pm. We give will give short extensions to groups that run into trouble. But us:

Instruction Set Architectures

General Purpose Processors

EC 413 Computer Organization

CS5460: Operating Systems Lecture 14: Memory Management (Chapter 8)

EC-801 Advanced Computer Architecture

UNIT 8 1. Explain in detail the hardware support for preserving exception behavior during Speculation.

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Computer Organization CS 206 T Lec# 2: Instruction Sets

CS4617 Computer Architecture

CPU Architecture and Instruction Sets Chapter 1

T Reverse Engineering Malware: Static Analysis I

Instruction Set Principles and Examples. Appendix B

Computer Architecture. Chapter 2-2. Instructions: Language of the Computer

Computer Architecture

Lecture 4: Memory Management & The Programming Interface

Instruction Set Principles. (Appendix B)

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

BASIC COMPUTER ORGANIZATION. Operating System Concepts 8 th Edition

Instruction Set Architectures

T Jarkko Turkulainen, F-Secure Corporation

Reminder: tutorials start next week!

Multiple Instruction Issue. Superscalars

ECE 574 Cluster Computing Lecture 8

Lecture 4: RISC Computers

CMSC 611: Advanced Computer Architecture

ECE 552 / CPS 550 Advanced Computer Architecture I. Lecture 15 Very Long Instruction Word Machines

CSE506: Operating Systems CSE 506: Operating Systems

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

Computer Systems A Programmer s Perspective 1 (Beta Draft)

Lecture 4: MIPS Instruction Set

Digital System Design Using Verilog. - Processing Unit Design

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

EJEMPLOS DE ARQUITECTURAS

CENG3420 Lecture 03 Review

Chapter 2. Instruction Set. RISC vs. CISC Instruction set. The University of Adelaide, School of Computer Science 18 September 2017

Assembly Language Programming Linkers

Transcription:

ECE 498 Linux Assembly Language Lecture 1 Vince Weaver http://www.eece.maine.edu/ vweaver vincent.weaver@maine.edu 13 November 2012

Assembly Language: What s it good for? Understanding at a low-level what your computer is doing. It separates the Computer Engineers from the CS majors. Shown when using a debugger Compiler writers must translate higher languages to assembly Operating system writers (some things not expressible in C) 1

Embedded systems (code density) Research. Computer Architecture. Emulators/Simulators. Video games (or other perf critical routines, glibc, kernel, etc.) 2

Benefits of Using Linux Tools! In-place debugging tools Flat 32-bit address space I/O, timing, device drivers, all handled for you The O/S acts as a cross-platform abstraction layer 3

Other OSes If the architecture is the same, the assembly language is the same. All that changes is how you call the O/S. DOS Windows BSD/OSX/Other UNIX 4

Tools assembler: GNU Assembler as (others: tasm, nasm, masm, etc.) creates object files linker: ld creates executable files. resolves addresses of symbols. shared libraries. 5

Executable Format ELF Linux header, then header table describing chunks and where they go Other executable formats: a.out, COFF 6

ELF Layout ELF Header Program header Text (Machine Code) Data (Initialized Data) Symbols Debugging Info... Section header 7

ELF Background Executable and Linker Format ELF Header includes a magic number saying it s ELF, architecture type, OS type etc. Program Header has info telling the OS what parts to load and where Program Data follows: machine code, initialized data Other data: things like symbol names, debugging info 8

(DWARF), etc. Section Header (tells other tools what sections are where) 9

Loader /lib/ld-linux.so.2 loads the executable 10

Static vs Dynamic Libraries Static: includes all code in one binary. Large binaries, need to recompile to update library code, self-contained Dynamic: library routines linked at load time. Smaller binaries, share code across system, automatically links against newer/bugfixes 11

How a Program is Loaded Kernel Boots init started init calls fork() child calls exec() Kernel checks if valid ELF. Passes to loader Loader loads it. Clears out BSS. Sets up stack. Jumps 12

to entry address (specified by executable) Program runs until complete. Parent process returned to if waiting. Otherwise, init. 13

What a program s memory layout looks like Operating System Exexcutable Info Stack shared libraries vdso mmap Heap BSS Data 0xffff ffff 0xbfff ffff Operating System Environment Strings Cmd Line Arg Strings Executable Name Padding ELF Auxiliary Vectors Environment Pointers Command Line Pointers Cmd Line Arg Count Stack Text (Executable) 0x0804 8000 Null Guard Page 0x0000 0000 14

What a program s memory layout looks like Text: the program s raw machine code Data: Initialized data BSS: uninitialized data; on Linux this is all set to 0. Heap: dynamic memory. malloc() and brk(). Grows up Stack: LIFO memory structure. Grows down. 15

Program Layout Kernel: is mapped into top of address space, for performance reasons Command Line arguments, Environment, AUX vectors, etc., available above stack For security reasons ASLR (Address Space Layout Randomization) is often enabled. From run to run the exact addresses of all the sections is randomized, to make it harder for hackers to compromise your system. 16

Brief overview of Virtual Memory Each program gets a flat 4GB (on 32-bit) memory space. The CPU and Operating system work together to provide this, even if not that much RAM is available and even though different processes seem to be using the same addresses. Physical vs Virtual Memory OS/CPU deal with pages, usually 4kB chunks of memory. 17

Every mem access has to be translated. The operating system looks in the page table to see which physical address your virtual address maps to. This is slow. That s where TLB comes in; it caches pagetable translations. As long as you don t run out of TLB entries this goes fast. Demand paging: the OS doesn t have to load pages into memory until the first time you actually load/store them. Context Switch: when you switch to a new program, 18

the TLB is flushed and a different page table is used to provide the new program its own view of memory. 19

What you have at entry Registers Instruction pointer at beginning Stack command line arguments, aux, environment variables Large contiguous VM space 20

RISC / CISC / VLIW RISC: Reduced Instruction Set Computer Small set of instructions to make processor design simpler. Usually fixed-length instructions, load/store CISC: Complex Instruction Set Computer Wide ranging complicated instructions; have complicated CPU decode circuitry. Often variable length instructions. Often allow operating on memory directly. VLIW: Very Long Instruction Word 21

Instructions come in long bundles, often 3 at a time. Cannot have dependencies; may have to fill with nops. Allows compiler to exploit inherit parallelism in code (most modern CPUs do this in hardware instead, VLIW puts this complexity in software). 22

CISC/RISC/VLIW Examples MIPS is RISC: roughly only 40 integer instructions, (more if you include FP) x86 is CISC: hundreds of complicated instructions, including ones that access memory, auto-increment registers, have complex shift/add address modes Hybrid: ARM or Power started out RISC but have accumulated more complicated instructions over time 23

x86, while CISC externally, internally decodes to a RISClike code before executing 24

Converting Assembly to Machine Language MIPS - 4 bytes ooooooss sssttttt dddddaaa aaffffff o6: Instruction opcode s5: First source register t5: Second source register d5: Destination register 25

a5: Shift amount, for shift instructions. f6: Function. 26

Converting Assembly to Machine Language x86: 1-15 bytes LOCK prefix REP prefix SEGMENT prefix 16/32/64 bit (REX) prefix Opcode 27

VEX/XOP byte MODRM (register or memory operand) SIB scaling factor displacement, scaling factor 28

Things to determine when writing assembly Can find this in reference books. Manuals online for free Intel 64 and IA-32 Architectures Developer s Manual, huge. Similar from AMD. ARM ARM. Instruction names (and what they do) Number of registers, Register names (or aliases), orthogonality of registers. ABI: Registers uses (which to save, which are parameters, 29

stack pointer, instruction pointer, zero pointer, multiply results, floating point, return address) Endianess How to make a syscall if opcode takes 2 or 3 arguments Load/store vs otherwise Flags / Condition Codes instruction width 30

Addressing Modes 31

Weirder features to watch for Register windows branch delay slot predication unaligned loads byte access (alpha) HW divide 32

zero register sign-extend (especially x86 vs x86 64) 33

Types of Instructions ALU: add/addu/sub/subu/addi/addiu/mult/multu/div/div Memory: lw,lh,lhu,lb,lbu,sw,sh,sb,lui,mfc,mtc,mfhi,mflo Logical: and,andi,or,ori,xor,nor,slt,slti Shift: sll,srl,sra Cond Branch: beq,bne Uncond Jump: j, jr, jal 34

HW Specific (System Calls, cache flush, TLB, perf counter, etc) 35