Instructions in Machine Language

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

Instruction Set-Intro
Systems Architecture Lecture 5: MIPS Instruction Set
Chapter 2 Instructions: Language of the Computer
Chapter 2.
CS 61C L13Introduction to MIPS: Instruction Representation I (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Chapter 2 Instructions: Language of the Computer Part III.
The CPU Revision Typical machine code instructions Using op-codes and operands Symbolic addressing. Conditional and unconditional branches.
CS61C L05 Introduction to MIPS Assembly Language : Arithmetic (1) Garcia, Fall 2011 © UCB Lecturer SOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
COMP3221 lec08-arith.1 Saeid Nooshabadi COMP 3221 Microprocessors and Embedded Systems Lecture 8: C/Assembler Data Processing
CS 61C L06 MIPS Intro (1) A Carle, Summer 2005 © UCB inst.eecs.berkeley.edu/~cs61c/su05 CS61C : Machine Structures Lecture #6: Intro to MIPS
CS61C L08 Introduction to MIPS Assembly Language: Arithmetic (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Normal C Memory Management °A program’s address space contains 4 regions: stack: local variables, grows downward heap: space requested for pointers via.
ECE 15B Computer Organization Spring 2010 Dmitri Strukov Lecture 4: Arithmetic / Data Transfer Instructions Partially adapted from Computer Organization.
CIS 314: Introduction to MIPS Assembly Language: Arithmetic Fall 2005 I´ve been getting this a lot lately So, what are you teaching this term? Computer.
CS61C L13 Introduction to MIPS: Instruction Representation I (1) Garcia © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
1 Lecture 2: MIPS Instruction Set Today’s topic:  MIPS instructions Reminder: sign up for the mailing list cs3810 Reminder: set up your CADE accounts.
CS1104 Assembly Language: Part 1
CS 61C L08 Introduction to MIPS Assembly Language: Arithmetic (1) Garcia, Spring 2004 © UCB Lecturer PSOE Dan Garcia inst.eecs.berkeley.edu/~cs61c.
Instruction Representation I (1) Fall 2005 Lecture 05: INSTRUCTION REPRESENTATION.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 13 MIPS Instruction Representation I The National Science Foundation (NSF)
From C to Assembly Language Jen-Chang Liu, Spring 2006 Adapted from
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Topic 10: Instruction Representation CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and.
Reformatting a Machine 125 Million Miles Away! Mars rover “Opportunity” got its flash drive reformatted 9/8/14, reason: rover keeps rebooting. Happy news:
Lecture 4: MIPS Instruction Set Reminders: –Homework #1 posted: due next Wed. –Midterm #1 scheduled Friday September 26 th, 2014 Location: TODD 430 –Midterm.
Module : Algorithmic state machines. Machine language Machine language is built up from discrete statements or instructions. On the processing architecture,
Chapter 2 Instructions: Language of the Computer Part I.
CHAPTER 6 Instruction Set Architecture 12/7/
CS2100 Computer Organisation MIPS Part I: Introduction (AY2015/6) Semester 1.
CENG 311 Instruction Representation
Computer Organization Instructions Language of The Computer (MIPS) 2.
CHAPTER 2 Instruction Set Architecture 3/21/
CS61C L05 Introduction to MIPS Assembly Language : Arithmetic (1) Garcia, Spring 2014 © UCB TA/Guest Lecturer: Shreyas Chand inst.eecs.berkeley.edu/~cs61c.
Assembly Language Basic job of a CPU: execute lots of instructions. Instructions are the primitive operations that the CPU may execute. Different CPUs.
Instructor: Dr. Mike Turi Department of Computer Science and Computer Engineering Pacific Lutheran University Lecture slides adapted from Part 4, EE 334.
Computer Architecture. Instruction Set “The collection of different instructions that the processor can execute it”. Usually represented by assembly codes,
1 What we want: execute High Level Language (HLL) programs What we have: computer hardware (a glorified calculator)
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Instructor: Dr. Mike Turi Department of Computer Science and Computer Engineering Pacific Lutheran University Lecture slides adapted from Part 8, EE 334.
Inst.eecs.berkeley.edu/~cs61c UCB CS61C : Machine Structures Lecture 08 MIPS Instruction Representation I Guest Lecturer Alan Christopher
CS2100 Computer Organisation
Computers’ Basic Organization
MIPS Assembly.
IT 251 Computer Organization and Architecture
Lecturer PSOE Dan Garcia
IT 251 Computer Organization and Architecture
Instruction Set Architecture
Morgan Kaufmann Publishers
Lecturer PSOE Dan Garcia
IT 251 Computer Organization and Architecture
IT 251 Computer Organization and Architecture
Instructions - Type and Format
Lecture 4: MIPS Instruction Set
Systems Architecture Lecture 5: MIPS Instruction Set
“so many gadgets, so many aches” - NYT
The University of Adelaide, School of Computer Science
ECE232: Hardware Organization and Design
C second, objective-c, Go up!
Hello to Jessy VanDivner listening from Ohio!
Instructions and Conditional Execution
Computer Architecture
3.
UCSD ECE 111 Prof. Farinaz Koushanfar Fall 2018
COMS 361 Computer Organization
COMS 361 Computer Organization
Inst.eecs.berkeley.edu/~cs61c CS61C : Machine Structures Lecture 13 – Introduction to MIPS Instruction Representation I Lecturer PSOE.
March 2006 Saeid Nooshabadi
Instruction Set Architecture
Presentation transcript:

Instructions in Machine Language Prof. Giancarlo Succi, Ph.D., P.Eng. E-mail: Giancarlo.Succi@unibz.it Prof. G. Succi, Ph.D., P.Eng.

On per for man ces … Prof. G. Succi, Ph.D., P.Eng.

Content Review Again on the Stored Program concept Program preparation and structure of the instructions Simple math operations Prof. G. Succi, Ph.D., P.Eng.

Review: the Instruction set Machine specific language: Lowest level of programming Read and executed by the CU Ex. add R1, R2, R3 Instruction set used here: MIPS Prof. G. Succi, Ph.D., P.Eng.

Review: 5 components More in detail… Computer Processor Memory Devices Control Datapath More in detail… Prof. G. Succi, Ph.D., P.Eng.

Content Review Again on the Stored Program concept Program preparation and structure of the instructions Simple math operations Prof. G. Succi, Ph.D., P.Eng.

Stored program concept Processor Memory PC CU program data ALU Registers The PC is a “Special” register Program execution: step by step of machine language instructions Prof. G. Succi, Ph.D., P.Eng.

The Stored-Program Concept Computers built on 2 key principles: 1) Instructions are represented as numbers. 2) Therefore, entire programs can be stored in memory to be read or written just like numbers (data). Simplifies SW/HW of computer systems: Memory technology for data also used for programs Prof. G. Succi, Ph.D., P.Eng.

Consequence #1: Everything Addressed Since all instructions and data are stored in memory as numbers, everything has a memory address: instructions, data words both branches and jumps use these Java references are just memory addresses: they can point to anything in memory Unconstrained use of addresses can lead to nasty bugs; up to you in C; limits in Java One register keeps address of instruction being executed: “Program Counter” (PC) Basically a pointer to memory: Intel calls it Instruction Address Pointer, which is better Prof. G. Succi, Ph.D., P.Eng.

Consequence #2: Binary Compatibility Programs are distributed in binary form Programs bound to specific instruction set Different version for Macintosh and IBM PC New machines want to run old programs (“binaries”) as well as programs compiled to new instructions Leads to instruction set evolving over time Selection of Intel 8086 in 1981 for 1st IBM PC is major reason latest PCs still use 80x86 instruction set (Pentium III); could still run program from 1981 PC today Prof. G. Succi, Ph.D., P.Eng.

Content Review Again on the Stored Program concept Program preparation and structure of the instructions Simple math operations Prof. G. Succi, Ph.D., P.Eng.

Program preparation: compiler C-program Swap (int v[], int k) ... Machine language program compiler 0010000010100011 1011110110101111 ... Compiler decides: Which instructions to use Which registers to use Prof. G. Succi, Ph.D., P.Eng.

Instructions as Numbers (1/2) Currently all data we work with is in words (32-bit blocks): Each register is a word. lw and sw both access memory one word at a time. So how do we represent instructions? Remember: Computer only understands 1s and 0s, so “add $t0,$0,$0” is meaningless. MIPS wants simplicity: since data is in words, make instructions be words... Lw and sw are two operations to access words in memory: load word and store word Prof. G. Succi, Ph.D., P.Eng.

Instructions as Numbers (2/2) One word is 32 bits, so divide instruction word into “fields”. Each field tells computer something about instruction. We could define different fields for each instruction, but MIPS is based on simplicity, so define 3 basic types of instruction formats: R-format I-format J-format Prof. G. Succi, Ph.D., P.Eng.

Internal representation Elements of an addition Source register 1 Source register 2 Target register Operation type Example: add $8, $17, $18 operation1 source1 source2 destination unused operation2 17 18 8 32 000000 10001 10010 01000 00000 100000 Prof. G. Succi, Ph.D., P.Eng.

Operands Variables Registers MIPS Variables are mapped to registers Registers can be accessed directly from the program Registers Fixed, identical length (typical: 32 bits) Fixed number (typical 32) MIPS 32 registers with 32 bits 30 usable, 2 with special meaning Prof. G. Succi, Ph.D., P.Eng.

Instructions: Groups Arithmetic operations Information flow Addition, Subtraction,... Information flow Load from memory Store in memory Logic operations Logic and / or Negation Shift Branch operation Prof. G. Succi, Ph.D., P.Eng.

Instructions: Types Instructions with different numbers of operands Jump # address Jump $ register number 2 Operands: Multiply $2, $3 Multiply $2 and $3 and store the result in ($2,$3) 3 Operands: Add a, b, c # a = b + c Add a, a, b # a = a + b Sub a, b ,c # a = b - c Prof. G. Succi, Ph.D., P.Eng.

Assembly Variables: Registers (1/3) Unlike HLL, assembly cannot use variables Why not? Keep Hardware Simple Assembly Operands are registers limited number of special locations built directly into the hardware operations can only be performed on these! Benefit: Since registers are directly in hardware, they are very fast HLL = High Level Languages Prof. G. Succi, Ph.D., P.Eng.

Assembly Variables: Registers (2/3) Drawback: Since registers are in hardware, there are a predetermined number of them Solution: MIPS code must be very carefully put together to efficiently use registers 32 registers in MIPS Why 32? Smaller is faster Each MIPS register is 32 bits wide Groups of 32 bits called a word in MIPS Prof. G. Succi, Ph.D., P.Eng.

Assembly Variables: Registers (3/3) Registers are numbered from 0 to 31 Number references: $0, $1, $2, … $30, $31 By convention, each register also has a name to make it easier to code: $16 - $23  $s0 - $s7 (correspond to Java variables) $8 - $15  $t0 - $t7 (correspond to temporary variables) In general, use register names to make your code more readable Note here these numbers… Prof. G. Succi, Ph.D., P.Eng.

Assembly Design: Key Concepts Assembly language is essentially directly supported in hardware, therefore ... It is kept very simple! Limit on the type of operands Limit on the set operations that can be done to absolute minimum if an operation can be decomposed into a simpler operation, don’t include it Prof. G. Succi, Ph.D., P.Eng.

Comments in Assembly Another way to make your code more readable: comments! Hash (#) is used for MIPS comments anything from hash mark to end of line is a comment and will be ignored Note: Different from Java The equivalent of # is // Java comments have also the format /* comment */ , so they can span many lines Prof. G. Succi, Ph.D., P.Eng.

Assembly Instructions In assembly language, each statement (called an Instruction), executes exactly one of a short list of simple commands Unlike Java (and most other High Level Languages), where each line could represent multiple operations Prof. G. Succi, Ph.D., P.Eng.

Content Review Again on the Stored Program concept Program preparation and structure of the instructions Simple math operations Prof. G. Succi, Ph.D., P.Eng.

Review of Java Operators/Operands Operators: +, -, *, /, % (mod); 7/4==1, 7%4==3 Operands: Variables: fahr, celsius Constants: 0, 1000, -17, 15.4 Assignment Statement: Variable = expression Examples: celsius = 5*(fahr-32)/9; a = b+c+d-e; Prof. G. Succi, Ph.D., P.Eng.

Addition and Subtraction (1/3) Syntax of Instructions: 1 2,3,4 where: 1) operation by name 2) operand getting result (“destination”) 3) 1st operand for operation (“source1”) 4) 2nd operand for operation (“source2”) Syntax is rigid: 1 operator, 3 operands Why? Keep Hardware simple via regularity Prof. G. Succi, Ph.D., P.Eng.

Addition and Subtraction (2/3) Addition in Assembly Example: add $s0, $s1, $s2 (in MIPS) Equivalent to: a = b + c (in Java) where registers $s0,$s1,$s2 are associated with variables a, b, c Subtraction in Assembly Example: sub $s3, $s4, $s5 (in MIPS) Equivalent to: d = e - f (in Java) where registers $s3,$s4,$s5 are associated with variables d, e, f Remember that: $16 - $22  $s0 - $s7 (correspond to Java variables) $8 - $15  $t0 - $t7 (correspond to temporary variables) Prof. G. Succi, Ph.D., P.Eng.

Addition and Subtraction (3/3) How do the following C statement? a = b + c + d - e; Break into multiple instructions add $s0, $s1, $s2 # a = b + c add $s0, $s0, $s3 # a = a + d sub $s0, $s0, $s4 # a = a - e Notice: A single line of Java may break up into several lines of MIPS. Notice: Everything after the hash mark on each line is ignored (comments) Prof. G. Succi, Ph.D., P.Eng.

Example Add the four variables b, c, d, and e and place the result in variable a. a = b + c + d + e Add operation, first parameter is the destination, second + third are the source Sequence: add $s0, $s1, $s2 # a = b + c add $s0, $s0, $s3 # a = b + c + d add $s0, $s0, $s4 # a = b + c + d + e Prof. G. Succi, Ph.D., P.Eng.

Proposed exercises Transform the following Java instructions into assembly code a = a + b – c; a = (b-c)+(e-f) a--; b+=c; a = 2*(b-c)+d; For the last one we do not need any 2!!! Prof. G. Succi, Ph.D., P.Eng.

Immediates Immediates are numerical constants They appear often in code, so there are special instructions for them Add Immediate: addi $s0,$s1,10 (in MIPS) f = g + 10 (in Java) where registers $s0,$s1 are associated with variables f, g Syntax similar to add instruction, except that last argument is a number instead of a register This is just the convention on numeric constants. Note that it is called “immediates” –plural. Prof. G. Succi, Ph.D., P.Eng.

Register Zero One particular immediate, the number zero (0), appears very often in code So we define register zero ($0 or $zero) to always have the value 0 Use this register, it’s very handy! add $6,$0,$5 # copy $5 to $6 This register is defined in hardware, so an instruction like addi $0,$0,5 will not do anything Prof. G. Succi, Ph.D., P.Eng.

Remember the different representations Assembler code: Mnemonic notation add $8, $18, $8 sub $9, $12, $15 Binary representation Sequence of 32 '1's and '0's 0111 0000 1001 0100 1010 1001 0100 1010 Prof. G. Succi, Ph.D., P.Eng.

More proposed exercises Transform the following Java instructions into assembly code a +=23; a = 2*(b+c) – 4 – d b = 27+15+d a = (4/3)*3.14*b*b*b Smart compilers already perform operations such as 27+15, when they can So far we cannot do the last one… Prof. G. Succi, Ph.D., P.Eng.