Virtual Machine Tutorial

Similar documents
S3.0 : A multicore 32-bit Processor

Talen en Compilers. Johan Jeuring , period 2. December 15, Department of Information and Computing Sciences Utrecht University

Practical Malware Analysis

SOURCE LANGUAGE DESCRIPTION

CS 11 C track: lecture 8

CISC324: Operating Systems. Assignment 1

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

Review Questions. 1 The DRAM problem [5 points] Suggest a solution. 2 Big versus Little Endian Addressing [5 points]

MOS 6502 Architecture

CMa simple C Abstract Machine

Lecture V Toy Hardware and Operating System

Course Administration

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

It is possible to define a number using a character or multiple numbers (see instruction DB) by using a string.

Binghamton University. CS-140 Fall Pippin

Project 3 Due October 21, 2015, 11:59:59pm

Registers. Registers

Programming of 8085 microprocessor and 8051 micro controller Study material

Computer Organization

Reverse Engineering Low Level Software. CS5375 Software Reverse Engineering Dr. Jaime C. Acosta

Code segment Stack segment

Accumulator and memory instructions 1. Loads, stores, and transfers 2. Arithmetic operations 3. Multiply and divide 4. Logical operations 5. Data test

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

CPSC 330 Computer Organization

ARM Architecture and Instruction Set

CG2007 Microprocessor systems.

CSIS1120A. 10. Instruction Set & Addressing Mode. CSIS1120A 10. Instruction Set & Addressing Mode 1

SOEN228, Winter Revision 1.2 Date: October 25,

TYPES OF INTERRUPTS: -

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

This simulated machine consists of four registers that will be represented in your software with four global variables.

LC-3 Instruction Set Architecture. Textbook Chapter 5

Microprocessors 1. The 8051 Instruction Set. Microprocessors 1 1. Msc. Ivan A. Escobar Broitman

Grundlagen Microcontroller Processor Core. Günther Gridling Bettina Weiss

EE 361 University of Hawaii Fall

Systems I. Machine-Level Programming V: Procedures

Processor design - MIPS

ORG ; TWO. Assembly Language Programming

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

Chapter 2A Instructions: Language of the Computer

Summer 2003 Lecture 4 06/14/03

Intermediate Representations

Computer Organization MIPS ISA

Module 3 Instruction Set Architecture (ISA)

Compiler Construction I

Microcontroller Intel [Instruction Set]

Quadsim Version 2.1 Student Manual

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

Problem with Scanning an Infix Expression

CS251 Programming Languages Handout # 29 Prof. Lyn Turbak March 7, 2007 Wellesley College

The MIPS Instruction Set Architecture

CS24: INTRODUCTION TO COMPUTING SYSTEMS. Spring 2018 Lecture 4

Instructions: Language of the Computer

DR bit RISC Microcontroller. Instructions set details ver 3.10

Midterm 2 Review Chapters 4-16 LC-3

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

Assembly Language Manual for the STACK Computer

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

Intel x86 instruction set architecture

Address Modes effective address

17. Instruction Sets: Characteristics and Functions

Design and Construction of a PC-Based Stack Machine Simulator for Undergraduate Computer Science & Engineering Courses

Instruction-set Design Issues: what is the ML instruction format(s) ML instruction Opcode Dest. Operand Source Operand 1...

CPE 323 MSP430 INSTRUCTION SET ARCHITECTURE (ISA)

Oct 13, 2015 Robert Heckendorn University of Idaho ======================================================================

Computer Architecture /

Computer Architecture and System Software Lecture 06: Assembly Language Programming

Assembly language Simple, regular instructions building blocks of C, Java & other languages Typically one-to-one mapping to machine language

UNIT 2 THE 8051 INSTRUCTION SET AND PROGRAMMING

SaM. 1. Introduction. 2. Stack Machine

ELEG3924 Microprocessor

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

EE2007 Microprocessor systems.

CSCE 5610: Computer Architecture

VARDHAMAN COLLEGE OF ENGINEERING (AUTONOMOUS) Shamshabad, Hyderabad

An assembler for Tcl bytecode:???? Kevin Kenny GE Research Ozgur Dogan Ugurlu University of San Francisco

Tutorial 1: Programming Model 1

INFOB3TC Exam 2. Sean Leather. Monday, 30 January 2012, 17:00 20:00

! What do we care about? n Fast program execution. n Efficient memory usage. n Avoid memory fragmentation. n Maintain data locality

W4118: PC Hardware and x86. Junfeng Yang

Assembly Language: Function Calls

8051 Overview and Instruction Set

F28HS2 Hardware-Software Interfaces. Lecture 6: ARM Assembly Language 1

INFOB3TC Solutions for Exam 2

ECE232: Hardware Organization and Design

x86 architecture et similia

Instruction Sets Ch 9-10

Instruction Sets Ch 9-10

ADVANCE MICROPROCESSOR & INTERFACING

Understand the factors involved in instruction set

Assembly Language: Function Calls" Goals of this Lecture"

Programming Model 2 A. Introduction

What Cannot be Read, Cannot be Leveraged?

CSc 520 Principles of Programming Languages

Computer Architecture

ELEG3923 Microprocessor Ch.3 Jump, Loop, and Call

The TinyJ Compiler's Static and Stack-Dynamic Memory Allocation Rules Static Memory Allocation for Static Variables in TinyJ:

Procedure Call. Procedure Call CS 217. Involves following actions

JAM 16: The Instruction Set & Sample Programs

Computer Architecture and Organization. Instruction Sets: Addressing Modes and Formats

Transcription:

Virtual Machine Tutorial CSA2201 Compiler Techniques Gordon Mangion

Virtual Machine A software implementation of a computing environment in which an operating system or program can be installed and run. http://searchservervirtualization.techtarget.com/definition/virtual-machine A process virtual machine is designed to run a single process (program). http://en.wikipedia.org/wiki/virtual_machine

Computing Environment Computing Environment CPU Memory I/O Input CPU Output Memory

Central Processing Unit CPU Registers Arithmetic Operations Logic Operations Flow Control of Programs Programs for one CPU usually do not work with another type of CPU

Virtual Machine Level Of Abstraction Virtual CPU Virtual Memory Model Controlled Input / Output

Virtual Machine Architecture Program (virtual machine program) Process / Application Virtual Machine VCPU Memory Abstraction Operating System CPU Memory Physical Hardware CPU Memory

Virtual Machine Architecture VCPU Either Or Register based Stack based Code Area Where Program(s) is loaded and executed Data Area Where program data is manipulated Stack

S-Machine VCPU Stack based 3 registers PC, SP, FP, Program Counter Stack Pointer Frame Pointer Code Area Where S-Machine Programs are loaded Stack Area All the operations are stack based Data is kept in the machine stack

S-Machine VCPU PC SP FP CodeArea Stack 00 00 00 00 01 00 00 04 00 00 00 00 0A 00 00 01 2 3 20 0

S-Machine Initial Conditions PC = 0x0000 SP = 0xFFFF FP = 0x0000 Code Area empty Stack empty

S-Machine Instructions A = 5 + 7 CodeArea Stack Registers ENTER 1 LDC 5 LDC 7 ADD STORE 0, 1 (SP) PC = 0 SP = -1 FP = 0

S-Machine Instructions A = 5 + 7 CodeArea Stack Registers ENTER 1 LDC 5 LDC 7 ADD STORE 0, 1 (SP) 0 PC = 1 SP = 0 FP = 0

S-Machine Instructions A = 5 + 7 CodeArea Stack Registers ENTER 1 LDC 5 LDC 7 ADD STORE 0, 1 (SP) 5 0 PC = 2 SP = 1 FP = 0

S-Machine Instructions A = 5 + 7 CodeArea Stack Registers ENTER 1 LDC 5 LDC 7 ADD STORE 0, 1 (SP) 7 5 0 PC = 3 SP = 2 FP = 0

S-Machine Instructions A = 5 + 7 CodeArea Stack Registers ENTER 1 LDC 1 LDC 2 ADD STORE 0, 1 (SP) 12 0 PC = 4 SP = 1 FP = 0

S-Machine Instructions A = 5 + 7 CodeArea Stack Registers ENTER 1 LDC 5 LDC 7 ADD STORE 0, 1 (SP) 12 PC = 5 SP = 0 FP = 0

S-Machine Instruction Set All Instructions are 32bits long (8 bytes) Format Opcode ( 8 bits) Scope Operand ( 8 bits ) Operand (16bits ) II ss nnnn

Instruction Set Examples 03000001 03 / 00 / 0001 STORE 0, 1 01000a00 01 / 00 / 000a LDC 10 00000000 00 / 00 / 0000 NOP Note:- Operands are in big-endian form.

Instruction Set - Arithmetic Arithmetic Operations ADD ( + ) (opcode 0x0F) SUB ( - ) (opcode 0x10) MUL ( * ) (opcode 0x11) DIV ( / ) (opcode 0x12) MOD ( % ) (opcode 0x13) Stack(SP-1) = Stack(SP-1) Op Stack(SP); SP = SP - 1; PC = PC + 1;

Instruction Set - Relational Relational Operations EQ (==) (opcode 0x0F) NE (!=) (opcode 0x10) LT (<) (opcode 0x11) GT (>) (opcode 0x12) LE (<=) (opcode 0x13) GE (>=) (opcode 0x13) Stack(SP-1) = Stack(SP-1) Op Stack(SP); SP = SP - 1; PC = PC + 1;

Instruction Set Operations NOP (opcode 0x00) No Operation HALT (opcode 0x19) Stop program execution WRITE (opcode 0x1B) Pop and Output the number on top of stack

Instruction Set Stack Stack Operations LDC n (opcode 0x01 00 nnnn) Load/Push Constant n on Stack DUP (opcode 0x04) Duplicate the top of stack item POP (opcode 0x05) Removes the item from the top of stack

Instruction Set Flow Control Flow Control Instructions JMP n (opcode 0x06 00 nnnn) Jump to location n in the code area JZ n (opcode 0x07 00 nnnn) If the top of stack item is 0 then jump to location n in the code area JNZ n (opcode 0x08 00 nnnn) If the top of stack item is NOT 0 then jump to location n in the code area

Instruction Set Flow Control Flow Control Instructions CALL n (opcode 0x0C 00 nnnn) Push [PC + 1] (next instruction location) on stack Jump to location n in the code area RET (opcode 0x0D ) Pop PC from the top of stack and jump to that address in the code area RETN n (opcode 0x0E 00 nnnn) Pop PC from the top of stack and jump to that address in the code area Pop n items from the stack

Instruction Set Remaining ENTER n (opcode 0x0A 00 nnnn) LEAVE (opcode 0x0B ) LD s, n STORE s, n (opcode 0x02 ss nnnn) (opcode 0x03 ss nnnn) READ s, n (opcode 0x1A ss nnnn)

Consider var int x = 0; x = x + 1; write x;

Variables and Scope var int x = 0; x = x + 1; write x; ENTER 1 LDC 0 STORE 0, 1 LD 0, 1 LDC 1 ADD STORE 0, 1 LD 0, 1 WRITE LEAVE

Variables and Scope ENTER 1 SP = SP + 1; Stack(SP) = FP; FP = SP; SP = SP + n; PC = PC + 1; Assumptions SP = 10 FP = 1 SP > FP > 9 8 7 6 5 4 3 2 1 0

Variables and Scope ENTER 1 SP = SP + 1; Stack(SP) = FP; FP = SP; SP = SP + n; PC = PC + 1; Assumptions SP = 10 FP = 1 SP > FP > @ 9 8 7 6 5 4 3 2 1 0

Variables and Scope ENTER 1 SP = SP + 1; Stack(SP) = FP; FP = SP; SP = SP + n; PC = PC + 1; SP > FP > 1 9 8 7 6 5 4 3 2 1 0

Variables and Scope ENTER 1 SP = SP + 1; Stack(SP) = FP; FP = SP; SP = SP + n; PC = PC + 1; SP / FP > 1 9 8 7 6 5 4 3 2 1 0

Variables and Scope ENTER 1 SP = SP + 1; Stack(SP) = FP; FP = SP; SP = SP + n; PC = PC + 1; SP > FP > @ 1 9 8 7 6 5 4 3 2 1 0

Variables and Scope ENTER 1 SP = SP + 1; Stack(SP) = FP; FP = SP; SP = SP + n; PC = PC + 1; SP > FP > @ 1 9 8 7 6 5 4 3 2 1 0

Variables and Scope var int x = 0; x = x + 1; write x; Location of x = Stack( FP + 1 ) ENTER 1 LDC 0 STORE 0, 1 LD 0, 1 LDC 1 ADD STORE 0, 1 LD 0, 1 WRITE LEAVE SP > FP > @ 1 9 8 7 6 5 4 3 2 1 0

Variables and Scope Local Variables are stored as positive offsets from the FP register Only the ENTER instruction modifies the FP register SP > FP > @ @ @ @ 1 < local var 4 = FP + 4 < local var 3 = FP + 3 < local var 2 = FP + 2 < local var 1 = FP + 1

Variables and Scope What about the local scope operand of LD s, n STORE s, n READ s, n (opcode 0x02 ss nnnn) (opcode 0x03 ss nnnn) (opcode 0x1A ss nnnn)

Variables and Scope ENTER instructions are executed FP changes but previous one is stored on the stack FP values are chained so the VM can backtrack to previous scopes Variables are referenced by the pair ( scope, offset ) FP > FP > FP > 1 9 3 7 6 5 4 0 2 1 0 L=2 L=1 L=0

Variables and Scope What about the local scope operand of LD s, n (opcode 0x02 ss nnnn) Pushes the value of variable (s,n) on the stack STORE s, n (opcode 0x03 ss nnnn) Stores the value on top of stack in the variable (s,n) READ s, n (opcode 0x1A ss nnnn) Reads an integer from standard input and stores the value in the variable (s,n)

Functions int Add(int a, int b) { var int n = 0; n = a + b; return n; }

Functions Definition int Add( int a, int b ) { } var int n = 0; n = a + b; return n; Call x = Add( 5, 7 );

Functions Definition int Add( int a, int b ) { } var int n = 0; n = a + b; return n; Call x = Add( 5, 7 ); Assumptions Let us assume that the Add function has been compiled, hence the type checker is happy and code generation is ready We know the address of the Add function We know that the Add function has a return value, 2 parameters and 1 local variable

Functions Signature int Add( int a, int b ) Consider Call Add( 5, 7 ); Prepare space for the return value Load/Push Parameters on the stack Perform the Function call Instructions LDC 0 // Prepare space LDC 5 // LD param 1 LDC 7 // LD param 2 CALL Add location // Call the function

Functions Remember CALL instruction pushes the PC on the stack Functions have a new scope so the generated code starts with an ENTER instruction

Functions Definition int Add( int a, int b ) { } var int n = 0; n = a + b; return n; Call x = Add( 5, 7 ); Instructions ENTER 1 LDC 0 LEAVE RETN 2 -------------------------------- LDC 0 LDC 5 LDC 7 CALL... Add

Functions Definition: int Add( int a, int b ) Call: Add( 5, 7 ); Instructions ENTER 1 LDC 0 LEAVE RETN 2 -------------------------------- LDC 0 LDC 5 LDC 7 @ FP (PC+1) 7 5 Local Variable Frame Pointer Return Location Param 2 Param 1 0 Return Value CALL... Add

Functions Accessing variables inside Functions Local Variable FP + 1 Param 1 FP 3 Param 2 FP 2 Return Value FP 4 Do Not Access FP 1 FP @ FP (PC+1) 7 5 Local Variable Frame Pointer Return Location Param 2 Param 1 0 Return Value

Instructions Functions Definition int Add( int a, int b ) { var int n = 0; n = a + b; return n; } Call x = Add( 5, 7 ); ENTER 1 LDC 0 STORE 0, 1 LD 0, -3 LD 0, -2 ADD STORE 0, 1 LD 0, 1 STORE 0, -4 LEAVE RETN 2 -------------------------------- LDC 0 LDC 5 LDC 7 @ FP (PC+1) 7 5 Local Variable Frame Pointer Return Location Param 2 Param 1 0 Return Value CALL... Add