Five classic components

Similar documents
Five classic components

syscall takes a service ID (number) in $v0 Read integer $v0=5, after syscall, $v0 holds the integer read from keyboard

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

CS/COE0447: Computer Organization

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Reduced Instruction Set Computer (RISC)

CS222: MIPS Instruction Set

Reduced Instruction Set Computer (RISC)

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

Chapter 2A Instructions: Language of the Computer

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

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

EEC 581 Computer Architecture Lecture 1 Review MIPS

CS/COE1541: Introduction to Computer Architecture

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

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

Instruction Set Architectures Part I: From C to MIPS. Readings:

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

MIPS%Assembly% E155%

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

Recap from Last Time. CSE 2021: Computer Organization. Levels of Programming. The RISC Philosophy 5/19/2011

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

5/17/2012. Recap from Last Time. CSE 2021: Computer Organization. The RISC Philosophy. Levels of Programming. Stored Program Computers

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Instructions: Language of the Computer

Computer Architecture

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Chapter 1. Computer Abstractions and Technology. Lesson 3: Understanding Performance

ECE260: Fundamentals of Computer Engineering

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

Computer Architecture. MIPS Instruction Set Architecture

CS3350B Computer Architecture MIPS Instruction Representation

Unsigned Binary Integers

Unsigned Binary Integers

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

ECE260: Fundamentals of Computer Engineering

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

ECE 154A Introduction to. Fall 2012

ECE369. Chapter 2 ECE369

Character Is a byte quantity (00~FF or 0~255) ASCII (American Standard Code for Information Interchange) Page 91, Fig. 2.21

Computer Organization MIPS ISA

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Chapter 3 MIPS Assembly Language. Ó1998 Morgan Kaufmann Publishers 1

COMPUTER ORGANIZATION AND DESIGN

Instruction Set Architecture of. MIPS Processor. MIPS Processor. MIPS Registers (continued) MIPS Registers

All instructions have 3 operands Operand order is fixed (destination first)

Computer Architecture. The Language of the Machine

CENG3420 Lecture 03 Review

Lecture 5: Procedure Calls

Computer Architecture

CS3350B Computer Architecture

CS3350B Computer Architecture MIPS Introduction

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Math 230 Assembly Programming (AKA Computer Organization) Spring MIPS Intro

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

CSCI 402: Computer Architectures

Topic Notes: MIPS Instruction Set Architecture

Chapter 2. Instruction Set Architecture (ISA)

CS31001 COMPUTER ORGANIZATION AND ARCHITECTURE. Debdeep Mukhopadhyay, CSE, IIT Kharagpur. Instructions and Addressing

ECE468 Computer Organization & Architecture. MIPS Instruction Set Architecture

Lecture 4: MIPS Instruction Set

Chapter 2: Instructions:

ECE232: Hardware Organization and Design

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Computer Organization and Components

CSEE 3827: Fundamentals of Computer Systems

CSCI 402: Computer Architectures. Instructions: Language of the Computer (3) Fengguang Song Department of Computer & Information Science IUPUI.

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

Instruction Set Architecture. "Speaking with the computer"

Instructions: Language of the Computer

Chapter 2. lw $s1,100($s2) $s1 = Memory[$s2+100] sw $s1,100($s2) Memory[$s2+100] = $s1

MIPS Datapath. MIPS Registers (and the conventions associated with them) MIPS Instruction Types

Part II Instruction-Set Architecture. Jan Computer Architecture, Instruction-Set Architecture Slide 1

Chapter 2. Instructions:

EE 109 Unit 13 MIPS Instruction Set. Instruction Set Architecture (ISA) Components of an ISA INSTRUCTION SET OVERVIEW

Chapter 4. The Processor. Computer Architecture and IC Design Lab

Forecast. Instructions (354 Review) Basics. Basics. Instruction set architecture (ISA) is its vocabulary. Instructions are the words of a computer

MIPS Coding Continued

CS 4200/5200 Computer Architecture I

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

Mark Redekopp, All rights reserved. EE 352 Unit 3 MIPS ISA

MACHINE LANGUAGE. To work with the machine, we need a translator.

MIPS Processor Overview

Computer Organization MIPS Architecture. Department of Computer Science Missouri University of Science & Technology

MODULE 4 INSTRUCTIONS: LANGUAGE OF THE MACHINE

Instruction Set Architecture

MIPS Instruction Set

Architecture I. Computer Systems Laboratory Sungkyunkwan University

We will study the MIPS assembly language as an exemplar of the concept.

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

CSE 141 Computer Architecture Spring Lecture 3 Instruction Set Architecute. Course Schedule. Announcements

MIPS Reference Guide

ECE 486/586. Computer Architecture. Lecture # 7

Transcription:

CS/COE0447: Computer Organization and Assembly Language Chapter 2 modified by Bruce Childers original slides by Sangyeun Cho Dept. of Computer Science Five classic components I am like a control tower I am like a pack of file folders I am like a conveyor belt + service stations I exchange information with outside world 2 1

MIPS operations and operands Operation specifies what function to perform by the instruction Operand specifies what quantity to use with the instruction MIPS operations operation: addition Arithmetic (integer/floating-point) Logical Shift Compare addi $t0,$t1,10 Load/store Branch/jump source operands System control and coprocessor MIPS operands Registers Fixed registers (e.g., HI/LO) destination operand Memory location Immediate value 3 MIPS arithmetic <op> <r target > <r source1 > <r source2 > All arithmetic instructions have 3 operands Operand order in notation is fixed; target first Two source registers and one target or destination register Operands are 2 registers or 1 register + 1 immediate Destination is a register Examples add $s1, $s2, $s3 # $s1 $s2 + $s3 sub $s4, $s5, $s6 # $s4 $s5 $s6 4 2

MIPS registers 32 bits 32 bits 32 bits $zero $at $v0 $v1 $a0 $a1 $a2 $a3 $t0 $t1 $t2 $t3 $t4 $t5 $t6 $t7 r0 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10 r11 r12 r13 r14 r15 r16 r17 r18 r19 r20 r21 r22 r23 r24 r25 r26 r27 r28 r29 r30 r31 $s0 $s1 $s2 $s3 $s4 $s5 $s6 $s7 $t8 $t9 $k0 $k1 $gp $sp $fp $ra HI LO PC General-Purpose Registers Special-Purpose Registers 5 General-purpose registers (GPRs) The name GPR implies that all these registers can be used as operands in instructions Still, conventions and limitations exist to keep GPRs from being used arbitrarily (from the PRM) $0, termed $zero, always has a value of 0 $31, termed $ra(return address), is reserved for storing the return address for subroutine call/return Register usage and related software conventions are typically summarized in application binary interface (ABI) important when writing system software such as an assembler or a compiler 32 GPRs in MIPS Are they sufficient? 6 3

Special-purpose registers HI/LO registers are used for storing result from multiplication operations PC (program counter) Always keeps the pointer to the current program execution point; instruction fetching occurs at the address in PC Not directly visible and manipulated by programmers in MIPS Other architectures May not have HI/LO; use GPRs to store the result of multiplication May allow storing to PC to make a jump 7 Instruction encoding Instructions are encoded in binary numbers Assembler translates assembly programs into binary numbers Machine (processor) decodes binary numbers to figure out what the original instruction is MIPS has a fixed, 32-bit instruction encoding Encoding should be done in a way that decoding is easy MIPS instruction formats R-format: arithmetic instructions I-format: data transfer/arithmetic/jump instructions J-format: jump instruction format (changes program counter) (FI-/FR-format: floating-point instruction format) 8 4

MIPS instruction formats Name Fields bit 31 bit 0 Comments Field Size 6 bits 5 bits 5 bits 5 bits 5 bits 6 bits All MIPS instructions 32 bits R-format op rs rt rd shamt funct Arithmetic/logic instruction format I-format op rs rt address/immediate Data transfer, branch, immediate format J-format op target address Jump instruction format 9 Instruction encoding example add $t0,$t1,$t8 $t8 is register 24 rt= 11000(5 bits) $t1is register r9 rs= 01001 (5 bits) $t0 is register r8 rd = 01000(5bits) op is addition opcode= 000000 (6 bits) funct= 100000 (6 bits) Resulting encoded instruction: shamt is unused shamt= 00000 (5 bits) Op Rs Rt Rd Shamt Funct 00000001001110000100000000100000 10 5

Instruction encoding example Instruction Format op rs rt rd shamt funct immediate add R 000000 reg reg reg 00000 100000 NA sub R 000000 reg reg reg 00000 100010 NA addi (add immediate) I 001000 reg reg NA NA NA constant lw (load word) I 100011 reg reg NA NA NA address offset sw (store word) I 101011 reg reg NA NA NA address offset 11 Logic instructions Name Fields Comments R-format op rs rt rd shamt funct Logic instruction format Bit-wise logic operations <op> <r target > <r source1 > <r source2 > Examples and $s3, $s2, $s1 # $s3 $s2 & $s1 or $t3, $t2, $t1 # $t3 $t2 $t1 nor $s3, $t2, $s1 # $s3 ~($t2 $s1) xor$s3, $s2, $s1 # $s3 $s2 ^ $s1 12 6

Dealing with immediate Name Fields Comments I-format op rs rt 16-bit immediate Transfer, branch, immediate format Many operations involve small immediate value a = a + 1 b = b 4 c = d 0x04 Some frequently used arithmetic/logic instruction shave their immediate version addi$s3, $s2, 16 # $s3 $s2 + 16 andi$s5, $s4, 0xfffe # $s5 $s4 & 1111111111111110b ori$t4, $t4, 4 # $t4 $t4 0000000000000100b xori$s7, $s6, 16 # $s7 $s6 ^ 0000000000010000b There is no subi ; why? 13 Handling long immediate number Sometimes we need a long immediate value, e.g., 32 bits Do we need an instruction to load a 32-bit constant value to a register? MIPS requires that we use two instructions lui $s3, 1010101001010101b $s3 1010101001010101 0000000000000000 Then we fill the low-order 16 bits ori $s3, $s3, 1100110000110011b $s3 1010101001010101 1100110000110011 14 7

Interacting with the OS We need the OS s help!!! How to print a number? (output) How to read a number? (input) How to terminate (halt) a program? How to open, close, read, write a file? These are operating system services Special instruction: syscall A software interrupt to invoke OS for an action (to do a service) Need to indicate the service to perform (e.g., print vs. terminate) May also need to pass an argument value (e.g., number to print) 15 A few useful syscalls syscall takes a service ID (number) in $v0 Print integer $v0=1, $a0=integer to print Read integer $v0=5, after syscall, $v0 holds the integer read from keyboard Print string $v0=4, $a0=memory address of string to print (null terminated) Exit (halt) $v0=10, no argument See MARS docs for more!!! Also, attend recitation. 16 8

Example: Print an integer # example as C program code # int a; # a = 10 + 8; # print( %d, a); # code below carries out the above li $t0,10 # $t0 is a, $t0=10 addi $t0,$t0,8 # $t0=10+8 li $v0,1 # print service add $a0,$t0,$0 # pass value in $t0 to service syscall # invoke OS to do service li $v0,10 # terminate program service syscall # invoke OS to do service Let s try it in MARS!!!! 17 Example: More useful output li $t0,10 # put 10 into a addi $t0,$t0,8 # do the add with 8 li $v0,4 # print string service la $a0,msg # load string syscall li $v0,1 # print integer ervice add $a0,$t0,$0 li $v0,10 # terminate program syscall # message to print before the number.data msg:.asciiz Sum of 10 + 8 is\n Let s try it in MARS!!!! 18 9

Example: Add 10 + x? li $v0,4 # print prompt la $a0,x_msg syscall li $v0,5 # read integer service syscall move $t0,$v0 # number read in $v0 addi $t0,$t0,10 # add number with 10 li $v0,4 # print result prompt la $a0,msg syscall li $v0,1 # print the sum move $a0,$t0 li $v0,10 # terminate program syscall.data x_msg:.asciiz Number x to add?\n msg:.asciiz Sum of 10 + x is\n Let s try it in MARS!!!! 19 Memory transfer instructions Also called memory access instructions Only two types of instructions Load: move data from memory to register e.g., lw$s5, 4($t6) # $s5 memory[$t6 + 4] Store: move data from register to memory e.g., sw $s7, 16($t3) # memory[$t3+16] $s7 In MIPS (32-bit architecture) there are memory transfer instructions for 32-bit word: int type in C 16-bit half-word: short type in C 8-bit byte: char type in C 20 10

Memory view Memory is a large, single-dimension 8-bit (byte) array with an address to each 8-bit item ( byte address ) A memory address is just an index into the array 0 1 2 3 4 5 BYTE #0 BYTE #1 BYTE #2 BYTE #3 BYTE #4 BYTE #5 address 4 gets this byte loads and stores give the index (address) to access 6 BYTE #6 7 BYTE #7 8 BYTE #8 21 Memory view Memory is a large, single-dimension 8-bit (byte) array with an address to each 8-bit item ( byte address ) A memory address is just an index into the array 0 1 2 3 4 5 BYTE #0 BYTE #1 BYTE #2 BYTE #3 BYTE #4 BYTE #5 address 4 gets this halfword loads and stores give the index (address) to access 6 BYTE #6 7 BYTE #7 8 BYTE #8 22 11

Memory view Memory is a large, single-dimension 8-bit (byte) array with an address to each 8-bit item ( byte address ) A memory address is just an index into the array 0 1 2 3 4 5 BYTE #0 BYTE #1 BYTE #2 BYTE #3 BYTE #4 BYTE #5 address 4 gets this word loads and stores give the index (address) to access 6 BYTE #6 7 BYTE #7 8 BYTE #8 23 Address calculation Memory address is specified with (register, constant) pair Register to keep the base address Constant field to keep the offsetfrom the base address Address is, then (contents of register + offset) The offset can be positive or negative Suppose base register $t0=64, then: lw $t0, 12($t1) address = 64 + 12 = 76 lw $t0, -12($t1) address = 64-12 = 52 MIPS uses this simple address calculation method; other architectures such as PowerPC and x86 support different methods 24 12

Machine code example void swap(int v[], int k) { int temp; temp = v[k]; v[k] = v[k+1]; v[k+1] = temp; } swap: sll $t0, $a1, 2 add $t1, $a0, $t0 lw $t3, 0($t1) lw $t4, 4($t1) sw $t4, 0($t1) sw $t3, 4($t1) jr $ra Let s try it in MARS!!!! 25 Memory organization 32-bit byte address 2 32 bytes with byte addresses from 0 to 2 32 1 2 30 words with byte addresses 0, 4, 8,, 2 32 4 Words are aligned 2 least significant bits (LSBs) of an address are 0s Half words are aligned LSB of an address is 0 Addressing within a word Which byte appears first and which byte the last? Big-endian vs. little-endian 0 4 8 12 16 20 0 0 WORD WORD WORD WORD WORD WORD MSB LSB 0 1 2 3 MSB LSB 3 2 1 0 26 13

More on alignment A misaligned access lw$s4, 3($t0) 0 0 1 2 3 How do we define a word at address? Data in byte 0, 1, 2, 3 If you meant this, use the address 0, not 3 Data in byte 3, 4, 5, 6 If you meant this, it is indeed misaligned! Certain hardware implementation may support this; usually not If you still want to obtain a word starting from the address 3 get a byte from address 3, a word from address 4 and manipulate the two data to get what you want Alignment issue does not exist for byte access 4 4 5 6 7 8 8 9 10 11 27 More on alignment intmain() { inta[100]; inti; int*ptr; for (i= 0; i< 100; i++) A[i] = i; printf("address of A[0] = %8x\n", &A[1]); printf("address of A[50] = %8x\n", &A[50]); ptr= &A[50]; printf("address in ptr = %8x\n", ptr); printf("value pointed by ptr = %d\n", *ptr); address of A[0] = bffff2b4 address of A[50] = bffff378 address in ptr = bffff378 value pointed by ptr = 50 address in ptr = bffff379 Segmentation fault ptr= (int*)((unsigned int)ptr + 1); printf("address in ptr = %8x\n", ptr); printf("value pointed by ptr = %d\n", *ptr); } 28 14