NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter

Slides:



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

Assembly Language for x86 Processors 6th Edition Chapter 5: Procedures (c) Pearson Education, All rights reserved. You may modify and copy this slide.
1 Lecture 4: Procedure Calls Today’s topics:  Procedure calls  Large constants  The compilation process Reminder: Assignment 1 is due on Thursday.
Lecture 6 Machine Code: How the CPU is programmed.
Outline Learning Assembly by an Example.  Program Formats  Some Simple Instructions  Assemble and Execute Learning Another Example  Data Definition.
PC hardware and x86 3/3/08 Frans Kaashoek MIT
1 ICS 51 Introductory Computer Organization Fall 2006 updated: Oct. 2, 2006.
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.
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
S. Barua – CPSC 440 CHAPTER 2 INSTRUCTIONS: LANGUAGE OF THE COMPUTER Goals – To get familiar with.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
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.
CS2422 Assembly Language and System Programming High-Level Language Interface Department of Computer Science National Tsing Hua University.
CS2422 Assembly Language & System Programming November 7, 2006.
High-Level Language Interface Chapter 17 S. Dandamudi.
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.
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
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.
Runtime Environments Compiler Construction Chapter 7.
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, _,
Procedures and the Stack Chapter 5 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Module R3 Process Scheduling. Module R3 involves the creation of a simple “Round Robin” dispatcher. The successful completion of this module will require.
1 ICS 51 Introductory Computer Organization Fall 2009.
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.
Addressing Modes Chapter 6 S. Dandamudi To be used with S. Dandamudi, “Introduction to Assembly Language Programming,” Second Edition, Springer,
מבנה מחשב תרגול 4 מבנה התוכנית. 2 Introduction When we wrote: ‘int n = 10’; the compiler allocated the variable’s memory address and labeled it ‘n’. In.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
Chapter 2 Parts of a Computer System. 2.1 PC Hardware: Memory.
CE-2810 Dr. Mark L. Hornick 1 Mixing C and assembly Safety goggles on!
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2013/2014.
Functions/Methods in Assembly
Compiler Construction Code Generation Activation Records
X86 Assembly Language We will be using the nasm assembler (other assemblers: MASM, as, gas)
Introduction to Assembly II Abed Asi Extended System Programming Laboratory (ESPL) CS BGU Fall 2014/2015.
Arrays. Outline 1.(Introduction) Arrays An array is a contiguous block of list of data in memory. Each element of the list must be the same type and use.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
Calling Procedures C calling conventions. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Preocedures A closer look at procedures. Outline Procedures Procedure call mechanism Passing parameters Local variable storage C-Style procedures Recursion.
Microprocessors CSE- 341 Dr. Jia Uddin Assistant Professor, CSE, BRAC University Dr. Jia Uddin, CSE, BRAC University.
7-Nov Fall 2001: copyright ©T. Pearce, D. Hutchinson, L. Marshall Oct lecture23-24-hll-interrupts 1 High Level Language vs. Assembly.
Practical Session 3.
Lecture 3 Translation.
Assembly language programming
Assembly language.
Format of Assembly language
Computer Architecture and Assembly Language
143A: Principles of Operating Systems Lecture 4: Calling conventions
Introduction to Compilers Tim Teitelbaum
High-Level Language Interface
Stack Frames and Advanced Procedures
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Assembly Language Programming II: C Compiler Calling Sequences
MIPS Procedure Calls CSE 378 – Section 3.
EECE.3170 Microprocessor Systems Design I
Practical Session 4.
Multi-modules programming
Week 2: Buffer Overflow Part 1.
EECE.3170 Microprocessor Systems Design I
Other Processors Having learnt MIPS, we can learn other major processors. Not going to be able to cover everything; will pick on the interesting aspects.
Computer Architecture and System Programming Laboratory
Computer Architecture and System Programming Laboratory
Presentation transcript:

NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter

INDEX INTRODUCTION About ASMLOVE Why assembly? NASM (Netwide Assembler) EXAMPLE CODE C CALLING CONVENTIONS REVIEW OF C VARIABLE STORAGE TYPES

INTRODUCTION About ASMLOVE Why assembly?

The Introduce of ASMLOVE Since Documentation & programming about assembly Dedicated at INTEL PROCESSOR Independent of OS Also we are interested in OS kernel and making OS. We mainly have offline seminars.

Main purpose of ASMLOVE Get a better understanding of how computer really work at a lower level rather than in high level languages. We want to make much more efficient product with understanding both of hardware and software. We are not only interested in PC but also embedded system and micro-controller.

Why should we learn assembly? Sometimes (never all) code written in assembly can be faster and smaller than compiler generated code. ex) MMX/SSE tech. Assembly allows access to direct hardware features of the system that might be difficult or impossible to use from a higher level language. ex) small controller, boot-loader

Why should anyone learn assembly at all? (cont’d) Learning to program in assembly helps one gain a deeper understanding of how computers work. Learning to program in assembly helps one understanding better how compilers and high languages like C work. In fact, everyone rarely program in assembly because it takes too much time and very hard to port to other platforms. But we uses the ideas we learn from assembly.

NASM (Netwide Assembler)

Netwide Assembler (NASM) It supports a range of object file formats, including Linux and NetBSD/FreeBSD a.out, ELF, COFF, Microsoft 16-bit OBJ and Win32. Its syntax is designed to be simple and easy to understand, similar to Intel's but less complex. It supports Pentium, P6, MMX, 3DNow!, SSE and SSE2 opcodes, and has macro capability.

Netwide Assembler (NASM) (cont’d) Get the example code and document _id= pcasm-book.pdf Table of Contents Introduction Basic Assembly Language Bit Operations Subprograms Arrays Floating Point Structures and C++

Netwide Assembler (NASM) (cont’d) NASM install bw.exe bw.exe Example Source ASM_src.zip ASM_src.zip

Netwide Assembler (NASM) (cont’d)

Data directives (different to MASM) L1db0; byte L2dw1000; word L3db110101b; byte L4db12h; byte L5db17o; byte L6dd1A92h; double word L7resb1; uninitialized byte L8db'A'; ascii code = 'A' L9db0,1,2,3; 4 bytes L10db'w', 'o','r','d',0 ;string L11db'word', 0 L12times 100 db 0; 100 bytes of zero L13resw100; 100*2(word bytes)

Netwide Assembler (NASM) (cont’d) Data directives (different to MASM) Mov al, [L1] ;copy byte at L1 Mov eax, L1 ;eax = address of byte at L1 Mov [L1], ah ; copy ah into byte at L1 Mov eax, [L6] ; copy double word Add eax, [L6] ; eax = eax + double word at L6 Add [L6], eax ; double word at L6 += eax Mov al, [L6] ; copy first byte of double word at L6 into al Mov [L6], 1 ; operation size is not specified Mov dword [L6], 1 ; store a 1 at L6

EXAMPLE CODE

Example code

Example code (cont’d)

C CALLING CONVENTIONS

C Calling conventions The code that calls a subprogram and the subprogram itself must agree on how data will passed between them. These rules on how data will be passed are called calling conventions. C calling conventions For high-level code to interface with assembly language, the assembly language code must use the same conventions as the high-level language.

C Calling conventions (cont’d) PUSH : add data to the stack POP : removes data SS : the segment that contain the stack ESP : top of the stack

C Calling conventions (cont’d) Data can only be added in double word units PUSH inserts a double word on the stack by subtracting 4 from ESP And then stores the double word at [ESP] POP reads the double word at [ESP] And then adds 4 to ESP STACK can be used as a convenient place to store data temporarily Also used for making subprogram calls, passing parameters and local variables.

C Calling conventions (cont’d) Call subprogram CALL Make an unconditional jump to a subprogram And pushes the address of the next instruction on the stack RET Pops off an address And jumps to that address. When using this inst. It is very important that one manage the stack correctly so that the right number is popped off by the RET.

C Calling conventions (cont’d) ENTER - Make Stack Frame (80188+) Usage: ENTER locals,level Modifies flags: None Modifies stack for entry to procedure for high level language. "locals" specifies the amount of storage to be allocated on the stack. “level" specifies the nesting level of the routine. For the C calling convention level must be zero. Paired with the LEAVE instruction, this is a efficient method of entry and exit to procedures.

C Calling conventions (cont’d) LEAVE - Restore Stack for Procedure Exit Usage: LEAVE Releases the local variables created by the previous ENTER instruction by restoring SP and BP to their condition before the procedure stack frame was initialized.

C Calling conventions (cont’d) The parameters on the stack are not popped off by the subprogram. Since they have to pushed on the stack before the CALL instruction, the return address would gave to be popped off first Often the parameters will have to be used in several places in the subprogram. Usually they can not be kept in an register for the entire subprogram and would have to be stored in memory.

C Calling conventions (cont’d) main() ret = asm_main(7); asm_main enter 0, 0 add eax, [ebp+8] 7 asm_main(7)  push 7  call asm_main  Add esp, 4 100Ch 1008h ESP = 1008h

C Calling conventions (cont’d) main() ret = asm_main(7); asm_main enter 0, 0 add eax, [ebp+8] Return address 7 call asm_main 100Ch 1008h 1004h ESP = 1004h

C Calling conventions (cont’d) main() ret = asm_main(7); asm_main enter 0, 0 add eax, [ebp+8] EBP Return address 7 enter 0, 0  push ebp  mov ebp, esp 100ch 1008h 1004h 1000h ESP = 1000h EBP = 1000h

C Calling conventions (cont’d) main() ret = asm_main(7); read_int enter 4, 0 lea eax, [ebp-4] Local variable EBP Return address 7 enter 0, 0  push ebp  mov ebp, esp 100ch 1008h 1004h 1000h ESP = 9FCh EBP = 1000h 9FCh

C Calling conventions (cont’d) Local variable EBP Return address 7 Leave  mov esp, ebp  pop ebp 100ch 1008h 1004h 1000h ESP = 1004h 9FCh

C Calling conventions (cont’d) Local variable EBP Return address 7 ret  pop eip 100ch 1008h 1004h 1000h ESP = 1008h 9FCh

C Calling conventions (cont’d) Local variable EBP Return address 7 100ch 1008h 1004h 1000h 9FCh ESP ret = asm_main(7);  mov eax, 7  push eax  call asm_main  add esp. 4  mov [ebp-4], eax

C Calling conventions (cont’d) Local variable EBP Return address parameter Local variables ebp-4h ebp-8h ebp-Ch Function parameter ebp+8h ebp+Ch ebp+10h ESP EBP

C Calling conventions (cont’d) Interfacing Assembly with C Inline assembly code must be written in the format the compiler uses. So different compilers require different formats. Assembly routines are used. Direct access hareware features Assmebly libraries (MMX, linux/win)

C Calling conventions (cont’d) Saving registers C assumes that a subroutine maintains the values of the following registers EBX, ESI, EDI, EBP, CS, DS, SS, ES Usually these registers save at stack. use ‘PUSHA/POPA’ ‘PUSHF/POPF’

C Calling conventions (cont’d) Labels of functions Most C compilers prepend a single underscore character at the beginning of the names of functions and global/static variables. (asm_main => _asm_main) The linux gcc compiler does not prepend any character.

C Calling conventions (cont’d) Passing parameters The arguments of a function are pushed on the stack in the reverse order. The rules of the C calling conventions were specifically written to take any number of arguments. In printf function, always the address of format string is at EBP+8, not matter how many parameters are passed. So printf code can look at the format string to determine how many parameters should have been passed and look for them on the stack. Printf(“x = %d\n”);  Print out the double word value at [EBP + 12]

C Calling conventions (cont’d) Calculating address of local variables Linker find the address of a label defined in the data or bss segments. Calculating the address of a local variable or parameter on the stack is not straightforward. lea eax, [ebp-8] => EAX holds the address of second local variable.

C Calling conventions (cont’d) Returning values Return values are passed via registers. All integral types (char, int, enum..) are returned in the EAX (extended to signed/unsigned 32 bit). Pointer values are also stored in EAX. Floating point values are stored in the ST0.

C Calling conventions (cont’d) Example code sub3.asm

REVIEW OF C VARIABLE STORAGE TYPES

Review of C variable storage types Global Defined outside of any function and are stored at fixed memory locations (data/bss segments) and exist from the beginning of the program until the end. If declared as static, only the functions in the same module can access them. (not external) Static Local variables of a function but stored at fixed memory likes data/bss. Only be accessed by in the function they are defined in. Automatic Allocated in stack, unallocated when the function returns.

Review of C variable storage types Register Just dependent to compiler Volatile This keyword tells the compiler that the value of the variable may change any moment. Often a compiler might store the value of a variable in a register temporarily and use the register in place of the variable in a section of code It can not do these types of optimizations with volatile variables A common example of a volatile variable would be one could be altered by two threads of a multi-threaded program.