Computer Systems Lecture 9

Similar documents
EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 4 Solution

CNIT 127: Exploit Development. Ch 1: Before you begin. Updated

Practical Malware Analysis

Lab 8 The Stack (LIFO) Structure

Assembler Programming. Lecture 10

Objectives. ICT106 Fundamentals of Computer Systems Topic 8. Procedures, Calling and Exit conventions, Run-time Stack Ref: Irvine, Ch 5 & 8

Program Exploitation Intro

x86 assembly CS449 Fall 2017

Intel assembly language using gcc

Stack -- Memory which holds register contents. Will keep the EIP of the next address after the call

Assembly Language Programming: Procedures. EECE416 uc. Charles Kim Howard University. Fall

CMSC 313 Lecture 12 [draft] How C functions pass parameters

Machine and Assembly Language Principles

Computer Architecture and Assembly Language. Practical Session 3

SOEN228, Winter Revision 1.2 Date: October 25,

x86 assembly CS449 Spring 2016

What the CPU Sees Basic Flow Control Conditional Flow Control Structured Flow Control Functions and Scope. C Flow Control.

CSCI 334: Principles of Programming Languages. Computer Architecture (a really really fast introduction) Lecture 11: Control Structures II

Inline Assembler. Willi-Hans Steeb and Yorick Hardy. International School for Scientific Computing

The Compilation Process

UNIVERSITY OF CALIFORNIA, RIVERSIDE

Ethical Hacking. Assembly Language Tutorial

Ch. 11: References & the Copy-Constructor. - continued -

EECE.3170: Microprocessor Systems Design I Summer 2017

CPS104 Recitation: Assembly Programming

Assembly Language Lab # 9

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 21: Generating Pentium Code 10 March 08

Microprocessors ( ) Fall 2010/2011 Lecture Notes # 15. Stack Operations. 10 top

CS356: Discussion #6 Assembly Procedures and Arrays. Marco Paolieri

complement) Multiply Unsigned: MUL (all operands are nonnegative) AX = BH * AL IMUL BH IMUL CX (DX,AX) = CX * AX Arithmetic MUL DWORD PTR [0x10]

C and Programming Basics

Winter Compiler Construction T11 Activation records + Introduction to x86 assembly. Today. Tips for PA4. Today:

143A: Principles of Operating Systems. Lecture 5: Calling conventions. Anton Burtsev January, 2017

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING PREVIEW SLIDES 16, SPRING 2013

238P: Operating Systems. Lecture 3: Calling conventions. Anton Burtsev October, 2018

16.317: Microprocessor Systems Design I Fall 2014

administrivia today start assembly probably won t finish all these slides Assignment 4 due tomorrow any questions?

CSC 2400: Computer Systems. Towards the Hardware: Machine-Level Representation of Programs

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 7. Procedures and the Stack

Module 8: Atmega32 Stack & Subroutine. Stack Pointer Subroutine Call function

Tutorial 1: Programming Model 1

CSC 8400: Computer Systems. Machine-Level Representation of Programs

Digital Forensics Lecture 3 - Reverse Engineering

CMSC 313 Lecture 12. Project 3 Questions. How C functions pass parameters. UMBC, CMSC313, Richard Chang

Outline. Unresolved references

Functions. Ray Seyfarth. August 4, Bit Intel Assembly Language c 2011 Ray Seyfarth

Dr. Ramesh K. Karne Department of Computer and Information Sciences, Towson University, Towson, MD /12/2014 Slide 1

EC 413 Computer Organization

143A: Principles of Operating Systems. Lecture 4: Calling conventions. Anton Burtsev October, 2017

The x86 Architecture

The Stack. Lecture 15: The Stack. The Stack. Adding Elements. What is it? What is it used for?

ADVANCE MICROPROCESSOR & INTERFACING

Chapter 10. Programming in C

Lecture 15 Intel Manual, Vol. 1, Chapter 3. Fri, Mar 6, Hampden-Sydney College. The x86 Architecture. Robb T. Koether. Overview of the x86

W4118: PC Hardware and x86. Junfeng Yang

Islamic University Gaza Engineering Faculty Department of Computer Engineering ECOM 2125: Assembly Language LAB. Lab # 10. Advanced Procedures

Module 3 Instruction Set Architecture (ISA)

Towards the Hardware"

ECE220: Computer Systems and Programming Spring 2018 Honors Section due: Saturday 14 April at 11:59:59 p.m. Code Generation for an LC-3 Compiler

Functions. Systems Programming Concepts

CNIT 127: Exploit Development. Ch 2: Stack Overflows in Linux

X86 Addressing Modes Chapter 3" Review: Instructions to Recognize"

printf("this program adds the value 10 to a given integer number.\n\n");

Technical Questions. Q 1) What are the key features in C programming language?

UW CSE 351, Winter 2013 Midterm Exam

AS08-C++ and Assembly Calling and Returning. CS220 Logic Design AS08-C++ and Assembly. AS08-C++ and Assembly Calling Conventions

Today s Menu. >Use the Internal Register(s) >Use the Program Memory Space >Use the Stack >Use global memory

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING

Millions of instructions per second [MIPS] executed by a single chip microprocessor

Introduction to Reverse Engineering. Alan Padilla, Ricardo Alanis, Stephen Ballenger, Luke Castro, Jake Rawlins

Lab 3. The Art of Assembly Language (II)

Subprograms: Arguments

Procedure-Calling Conventions October 30

2003 LXI H, 42F2H ; this instruction store 42F2 in to the HL pair POP H ; store data from top of the stack to HL pair

Subprograms: Local Variables

Homework / Exam. Return and Review Exam #1 Reading. Machine Projects. Labs. S&S Extracts , PIC Data Sheet. Start on mp3 (Due Class 19)

PHY4635/5635 Spring Lecture 8: Program Control Instructions

Xuan Guo. Lecture XIX: Subroutines (2) CSC 3210 Computer Organization and Programming Georgia State University. March 31, 2015.

G Programming Languages - Fall 2012

Betriebssysteme und Sicherheit Sicherheit. Buffer Overflows

Course Administration

Programming in C - Part 2

Question 4.2 2: (Solution, p 5) Suppose that the HYMN CPU begins with the following in memory. addr data (translation) LOAD 11110

x86 architecture et similia

Chapter 3. Z80 Instructions & Assembly Language. Von Neumann Architecture. Memory. instructions. program. data

Lecture (05) x86 programming 4

A Fast Review of C Essentials Part I

ECE 471 Embedded Systems Lecture 8

Function Calls COS 217. Reading: Chapter 4 of Programming From the Ground Up (available online from the course Web site)

Part 7. Stacks. Stack. Stack. Examples of Stacks. Stack Operation: Push. Piles of Data. The Stack

CS165 Computer Security. Understanding low-level program execution Oct 1 st, 2015

The x86 Instruction Set

CSC 2400: Computing Systems. X86 Assembly: Function Calls"

real-time kernel documentation

Instruction Set Architecture

Lectures 5-6: Introduction to C

Y86 Processor State. Instruction Example. Encoding Registers. Lecture 7A. Computer Architecture I Instruction Set Architecture Assembly Language View

16.317: Microprocessor Systems Design I Spring 2015

Computer Science Final Examination Wednesday December 13 th 2006


Transcription:

Computer Systems Lecture 9

CPU Registers in x86

CPU status flags EFLAG: The Flag register holds the CPU status flags The status flags are separate bits in EFLAG where information on important conditions such as overflow, or carry, is recorded.

CPU status flags

CPU status flags Most often used flags are: S :sign Z: zero, result being zero C: carry, indicates an arithmetic carry O: arithmetic overflow error

How CPU flags operate

Inline Assembler For the practical sessions we will use the inline Assembler within Microsoft Visual C++ Inline means you can insert assembly code directly into C/C++ programs, compile and execute them No knowledge of C/C++ is assumed

Inline Assembler Inline assembly language is a very close relative to the language of MASM (Microsoft Macro assembler) In fact, almost any MASM expression can be used in inline assembly code Inline assembly code can not use MASM directives for the definition (declaration) of data types and objects Data types and objects can be defined `outside of an _asm block as C or C++ data types and objects

Inline Assembler Inline assembly code can refer to C or C++ variables by name: _asm mov eax, var ; stores the value of ; var in EAX This example illustrates two more aspects: One can use _asm without brackets, in which case it works for only one line A semicolon ; is used for comments in the assembly program. Alternatively, one can use // for comments

Inline Assembler An _asm block can call C functions, including C library routines We will use the C library routines scanf and printf for input and output in our programs To pass arguments to the printf routine we will use a stack. It is time to discuss in more detail the important role of the stack.

The Stack A Stack is a memory arrangement (data structure) for storing and retrieving information (values) The order of storing and retrieving values from the stack can be described as LIFO (last in, first out) An example of an alternative memory arrangement is a queue: FIFO

The Stack Every stack is equipped with two operations: Push and Pop

The Stack The stack is a simple but very useful data structure. Almost any assembly language has special instructions for implementing a stack In inline assembly language there are PUSH and POP instructions PUSH and POP operations use the stack pointer register ESP to hold the address of the item which is currently on top of the stack

PUSH and POP PUSH instruction works: First by decrementing the address in ESP Then using the ESP address to write the item to memory POP instruction: The item is read using the ESP address The address is incremented by the correct amount to remove the item from the stack

Using PUSH and POP

Adjusting the stack pointer Sometimes the stack pointer has to be adjusted explicitly by the programmer ADD ESP,4 ; take 4 bytes off the stack ; (see example in the Hello World program) SUB ESP,256 ; create 256 bytes of space

The stack as temporary store /* demonstration of the use of asm instructions within C prog */ #include <stdio.h> #include <stdlib.h> int main (void) { char format[] = "Hello World\n"; // declare variables in C _asm { mov ecx,10 // initialize loop counter Lj: push ecx // loop count index saved on stack lea eax,format // push eax // address of string, stack parameter call printf // use library code subroutine add esp,4 //clean 4 byte parameter off stack pop ecx //restore loop counter ready for test loop Lj // dec ECX, jmp back IF NZ } // back to C return 0; }

Stack as temporary store In the above example, the stack was used as a scratch-pad temporary store to keep the value of the loop counter and free the ECX register for use This is a common use of the stack as a temporary store for variables (values)

Another use: passing parameters int main (void) { char format[] = "Hello World\n"; // declare variables in C _asm { mov ecx,10 // initialize loop counter Lj: push ecx // loop count index saved on stack lea eax,format // push eax call printf add esp,4 // address of string, stack parameter // use library code subroutine //clean 4 byte parameter off stack pop ecx //restore loop counter ready for test loop Lj // dec ECX, jmp back IF NZ } // back to C return 0; }

Passing parameters The printf routine needs extra information on what to print and how to print it. This should be passed as parameters. It is done via the stack: push eax puts the address of the string to be printed on the stack call printf reads the address from the top of the stack and prints the string, but it does not remove this address from the stack add esp,4 cleans the head of the stack (remove address)

Readings [W] Sections 7.2, 7.3,7.5, 8.4, 8.5