CS 11 C track: lecture 8

Similar documents
Virtual Machine Tutorial

Practical Malware Analysis

SOEN228, Winter Revision 1.2 Date: October 25,

Registers. Registers

ECE232: Hardware Organization and Design

AGENDA Binary Operations CS 3330 Samira Khan

Arithmetic and Bitwise Operations on Binary Data

Adding Binary Integers. Part 5. Adding Base 10 Numbers. Adding 2's Complement. Adding Binary Example = 10. Arithmetic Logic Unit

Computer Architecture /

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

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

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Ex: Write a piece of code that transfers a block of 256 bytes stored at locations starting at 34000H to locations starting at 36000H. Ans.

Integer Representation

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

Computer Architecture Prof. Smruti Ranjan Sarangi Department of Computer Science and Engineering Indian Institute of Technology, Delhi

Project 3: RPN Calculator

SPRING TERM BM 310E MICROPROCESSORS LABORATORY PRELIMINARY STUDY

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

last time Assembly part 2 / C part 1 condition codes reminder: quiz

CS 24: INTRODUCTION TO. Spring 2015 Lecture 2 COMPUTING SYSTEMS

The CPU and Memory. How does a computer work? How does a computer interact with data? How are instructions performed? Recall schematic diagram:

CS 61c: Great Ideas in Computer Architecture

2) Using the same instruction set for the TinyProc2, convert the following hex values to assembly language: x0f

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Microprocessor and Assembly Language Week-5. System Programming, BCS 6th, IBMS (2017)

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Wednesday, February 4, Chapter 4

Chapter Two MIPS Arithmetic

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

CS 31: Introduction to Computer Systems. 03: Binary Arithmetic January 29

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2

Areas for growth: I love feedback

Lab 3. The Art of Assembly Language (II)

CS401 Assembly Language Solved Subjective MAY 03,2012 From Midterm Papers. MC

Topics Power tends to corrupt; absolute power corrupts absolutely. Computer Organization CS Data Representation

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

Lab8: SAM Assembler and Simulator

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

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

Summary: Direct Code Generation

More about Binary 9/6/2016

Intel x86 Jump Instructions. Part 5. JMP address. Operations: Program Flow Control. Operations: Program Flow Control.

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

TYPES OF INTERRUPTS: -

Summer 2003 Lecture 4 06/14/03

CS 2505 Computer Organization I

CS107 Handout 13 Spring 2008 April 18, 2008 Computer Architecture: Take II

Lecture 5. Announcements: Today: Finish up functions in MIPS

Tutorial 1: Programming Model 1

Under the Hood: Data Representations, Memory and Bit Operations. Computer Science 104 Lecture 3

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

Chapter 2A Instructions: Language of the Computer

Wednesday, September 13, Chapter 4

CS61 Section Solutions 3

Problem with Scanning an Infix Expression

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

EE 361 University of Hawaii Fall

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

The C Programming Language Guide for the Robot Course work Module

CS 61c: Great Ideas in Computer Architecture

Computer Architecture and System Software Lecture 02: Overview of Computer Systems & Start of Chapter 2

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

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

1/31/2017. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

Computer Architecture and System Software Lecture 06: Assembly Language Programming

CS 61C: Great Ideas in Computer Architecture. MIPS Instruction Formats

Binary Arithmetic CS 64: Computer Organization and Design Logic Lecture #2 Fall 2018

Chapter 10 And, Finally... The Stack. ACKNOWLEDGEMENT: This lecture uses slides prepared by Gregory T. Byrd, North Carolina State University

CISC324: Operating Systems. Assignment 1

9/2/2016. Expressions are Used to Perform Calculations. ECE 120: Introduction to Computing. Five Arithmetic Operators on Numeric Types

15-122: Principles of Imperative Computation, Fall 2015

Chapter 2. Instructions:


CPU: SOFTWARE ARCHITECTURE INSTRUCTION SET (PART

Programming Model 2 A. Introduction

SaM. 1. Introduction. 2. Stack Machine

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

Binary Arithmetic Intro to Assembly Language CS 64: Computer Organization and Design Logic Lecture #3

EC 333 Microprocessor and Interfacing Techniques (3+1)

3.0 Instruction Set. 3.1 Overview

CS 61C: Great Ideas in Computer Architecture Intro to Assembly Language, MIPS Intro

CS 31: Intro to Systems Binary Arithmetic. Kevin Webb Swarthmore College January 26, 2016

CS401 - Computer Architecture and Assembly Language Programming Glossary By

Branch Addressing. Jump Addressing. Target Addressing Example. The University of Adelaide, School of Computer Science 28 September 2015

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

AVR ISA & AVR Programming (I)

Computer Organization MIPS ISA

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

CS 107 Lecture 2: Bits and Bytes (continued)

real-time kernel documentation

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

PHYS 319. Things to do before next week's lab Whirlwind tour of the MSP430 CPU and its assembly language Activity 1.

Time: 8:30-10:00 pm (Arrive at 8:15 pm) Location What to bring:

CS 64 Week 1 Lecture 1. Kyle Dewey

Computer Organization & Assembly Language Programming. CSE 2312 Lecture 15 Addressing and Subroutine

Transcription:

CS 11 C track: lecture 8 n Last week: hash tables, C preprocessor n This week: n Other integral types: short, long, unsigned n bitwise operators n switch n "fun" assignment: virtual machine

Integral types (1) n Usually use int to represent integers n But many other integral (integer-like) types exist: n n n n n n n short long char unsigned int unsigned short unsigned long unsigned char

Integral types (2) n Two basic things that can vary: n n unsigned vs. signed (default) length: char, short, int, long n Note that char is an integral type n can always treat char as an 8-bit integer n Two basic questions: n n Why use unsigned types? When should we use shorter/longer integral types?

Integral types (2) n Why use unsigned types? n may be used for something that can't be negative n e.g. a length n gives you 2x the range due to last bit n may want to use it as an array of bits n so sign is irrelevant n C has lots of bitwise operators

Integral types (3) n When should we use shorter/longer integral types? n n to save space when we know range is limited when we know the exact number of bits we need n char always 8 bits n short usually 16 bits n int usually 32 bits (but sometimes 64) n long usually 32 bits (but sometimes 64) n guaranteed: length(char) < length(short) <= length (int) <= length(long)

Integral types (4) n unsigned by itself means unsigned int n Similarly it's legal to say n n n n short int unsigned short int long int unsigned long int n but usually we shorten by leaving off the int

Bitwise operators (1) n You don't need to know this for this lab! n But a well-rounded C programmer should know this anyway... n There are several "bitwise operators" that do logical operations on integral types bit-by-bit n OR ( ) (note difference from logical or: ) n AND ( & ) (note difference from logical and: &&) n XOR ( ^ ) n NOT ( ~ ) (note difference from logical not:!)

Bitwise operators (2) n bitwise OR ( ) and AND (&) work bit-bybit n 01110001 10101010 =? n 11111011 n 01110001 & 10101010 =? n 00100000 n NOTE: They don't do short-circuit evaluation like logical OR ( ) and AND (&&) do n because that wouldn't make sense

Bitwise operators (3) n bitwise XOR (^) also works bit-by-bit n 01110001 ^ 10101010 =? n 11011011 n Bit is set if one of the operand's bits is 1 and the other is 0 (not both 1s or both 0s)

Bitwise operators (4) n bitwise NOT (~) also works bit-by-bit n ~10101010 =? n 01010101 (duh) n Substitute 0 for 1 and 1 for 0

Bitwise operators (5) n Two other bitwise operators: n bitwise left shift ( << ) n bitwise right shift ( >> ) n 00001111 << 2 =? n 00111100 n 00111100 >> 2 =? n 00001111 n Can use to multiply/divide by powers of 2

switch (1) n Minor language feature: switch n Used to choose from multiple integer-valued possibilities n Cleaner than a series of if/else if/else statements

switch (2) n Common coding pattern: void do_stuff(int i) { if (i == 0) { printf("zero\n"); } else if (i == 1) { printf("one\n"); } else { printf("something else\n"); } }

switch (3) void do_stuff(int i) { switch (i) { case 0: printf("zero\n"); break; case 1: printf("one\n"); break; default: printf("something else\n"); break; } }

switch (4) n switch statements more convenient than if/ else if/else for many integer-valued cases n but not as general -- can only be used on integral types (int, char, etc.) n Lab 8 code contains one switch statement that you don't have to write n but you should understand it anyway

switch (5) switch (i) { case 0: /* Start here if i == 0 */ } printf("zero\n"); break; /* Exit switch here. */... /* other cases: 1, 2, 42 etc. */ default: /* if no case matches i */ printf("no match\n"); break;

switch (6) -- fallthrough switch (i) { case 0: /* Start here if i == 0 */ } printf("zero\n"); /* oops, forgot the break */ case 1: /* "fall through" from case 0 */ printf("one\n"); break; n Now, if i is 0 then prints "zero" and also "one"! n Sometimes this is desired, but usually just a bug

Lab 8: Virtual machine (1) n Where have you heard the term "virtual machine" before? n Java virtual machine n A "virtual microprocessor" n You define simple instructions for a mythical computer's assembly language n Program interprets them

Virtual machine (2) n Our virtual machine is very simple n Only data type will be int n All instructions will act on ints n Instructions include n arithmetic n control flow n memory access n printing

Virtual machine (3) n First need to define data structures for our virtual microprocessor: n instruction memory to hold instructions of program n registers to hold temporary results of computations n stack to hold results that are being operated on directly

Virtual machine (4) n Instruction memory contains 2 16 locations n = 65536 n Each location is a single byte (unsigned char) n How many bits do we need to represent all possible locations in instruction memory? n 16 n Can use an unsigned short for this n Called the "instruction pointer" or IP n Don't confuse with C's pointers! Not the same thing! n It's just an index into the instruction memory

Virtual machine (5) n 16 registers (temporary storage locations) n How many bits do we need to represent all possible locations in registers? n 4 n Can use an unsigned char for this n Registers are just an array of 16 ints

Virtual machine (6) n Stack which is 256 deep n How many bits do we need to represent all possible locations in stack? n 8 n Can use an unsigned char for this n called the "stack pointer" or SP n also not a pointer in the C sense, just an index n Stack is just an array of 256 ints

Push and pop (1) n Stack has two operations: push and pop n push puts a new value onto the stack n pop removes a value from the stack n Have to adjust stack pointer (SP) after push and pop n Stack pointer "points to" first UNUSED element of stack n starts at zero for empty stack n Top filled element in stack is "top of stack" (TOS)

Push and pop (2) Stack starts off empty; SP points to first unused location SP stack

Push and pop (3) push 10 onto stack SP TOS 10 stack

Push and pop (4) SP push 20 onto stack TOS 20 10 stack

Push and pop (5) pop stack; SP TOS 20 10 20 still there, but will be overwritten next push stack

Push and pop (6) SP TOS 30 10 stack push 30 onto stack; old value (20) gets overwritten

Push and pop (7) 30 pop twice; stack is now "empty" again SP 10 stack

VM instruction set (1) n VM instructions are often called "bytecode" n because they fit into a byte (8 bits) n represented as an unsigned char n Our VM has 14 different instructions n some take operands (some number of bytes) n some don't

VM instruction set (2) n Instructions: n NOP (0x00) does nothing ("No OPeration") n PUSH (0x01) PUSH <n> pushes the integer <n> onto the stack n POP (0x02) removes the top element on the stack n LOAD (0x03) LOAD <r> pushes contents of register <r> to the top of the stack n STORE (0x04) STORE <r> pops top of stack and puts contents into register <r>

Load (1) 42... 0 1... registers load 0 SP stack

Load (2) 42... 0 1... registers SP TOS 42 load 0; 42 pushed onto stack stack

Store (1) 42... 0 1... registers SP store 1 42 stack

Store (2) 42 42... 0 1... registers SP 42 stack store 1; topmost element of stack copied into register 1; stack popped

VM instruction set (3) n Control flow instructions: n JMP (0x05) JMP <i> sets the instruction pointer (IP) to <i> ("jump") n JZ (0x06) JZ <i> sets IP to <i> only if the top value on the stack (TOS) is zero; also pops stack ("jump if zero") n JNZ (0x07) JNZ <i> sets IP to <i> only if the TOS is not zero; also pops stack ("jump if nonzero")

VM instruction set (4) n Arithmetic instructions: n ADD (0x08) pops the top two entries in the stack, adds them, pushes result back n SUB (0x09) pops the top two entries in the stack, subtracts them, pushes result back n Watch order! Should be S2 S1 on TOS n MUL (0x0a) and DIV (0x0b) defined similarly

Sub (1) SP TOS 31 42 stack sub (before)

Sub (2) SP 31 sub (after) TOS 11 stack

VM instruction set (5) n Other instructions: n PRINT (0x0c) prints the TOS to stdout and pop TOS n STOP (0x0d) terminates the virtual program

Example program (1) n Program to generate factorial of 10 (10!) n Which means...? n 10 x 9 x 8 x 7 x 6 x 5 x 4 x 3 x 2 x 1 n = 3628800 n But we'll write a program in our virtual machine's language

Example program (2) n Register 0 will contain the count n Register 1 will contain the running total n Register 0 will start off at 10 n each step, will decrease by 1 n Register 1 will start off at 1 n each step, will be multiplied by register 0 contents n Continue until register 0 has 0 n result is in register 1

Example program (3) /* Initialize the registers. */ 1 push 10 2 store 0 3 push 1 /* Initialize result. */ 4 store 1 /* continued on next slide... */

push 10 registers... 0 1... SP TOS 10 stack

store 0 10... 0 1... registers SP 10 stack

push 1 10... 0 1... registers SP TOS 1 stack

store 1 10 1... 0 1... registers SP 1 stack

Example program (4) /* Put counter value on stack. * If it's 0, we're done; register 1 * contains the final value. */ 5 load 0 /* Load current count. */ 6 jz 16 /* if 0, jump to 2 */ /* 16 is the location of the instruction which is the target of the jz instruction i.e. where to jump to. */

Example program (5) /* result = result * count */ 7 load 1 8 load 0 9 mul 10 store 1 /* count = count - 1 */ 11 load 0 12 push 1 13 sub 14 store 0

load 1 10 1... 0 1... registers SP TOS 1 stack

load 0 10 1... SP 0 1... registers TOS 10 1 stack

mul 10 1... 0 1... registers SP 10 TOS 10 stack

store 1 10 10... 0 1... registers 10 SP 10 stack

load 0 10 10... 0 1... registers SP 10 TOS 10 stack

push 1 10 10... SP 0 1... registers TOS 1 10 stack

sub 10 10... 0 1... registers SP 1 TOS 9 stack

store 0 9 10... 0 1... registers 1 etc.... SP 9 stack

Repeating... n Registers start off as 10, 1 n Then become 9, 10 n 8, 10*9 n 7, 10*9*8 n... n 0, 10! n... and we're done.

Example program (6) /* Go back and loop until done. */ 15 jmp 5 /* When we get here, we're done. */ 16 load 1 17 print 18 stop /* End of program. */

Lab 8 n Program is given to you n You need to write the byte-code interpreter n Most of code is supplied; have to fill in the guts of the instruction-processing code n Looks complicated but actually is pretty easy n Watch out for error checking e.g. n n n popping an empty stack pushing to a full stack accessing non-existent register or instruction

Lab 8 -- error checking n One subtlety with stack pushes n If stack pointer is at 255, and you push onto stack, what is the new stack pointer value? n 0 n (256 is too large for an unsigned char) n But this is clearly incorrect n How to detect "stack overflow"? n Solution: If stack pointer is at location 255, a push is invalid

Finally... n Hope you enjoyed the course! n If so, consider taking n other CS 11 tracks n (C++, java, advanced C/C++/java, python, ocaml, haskell) n CS 11 project track n CS 24 n CS 2, 3 for larger-scale software projects