Chapter 3 MIPS Assembly Language Morgan Kaufmann Publishers

Similar documents
Reduced Instruction Set Computer (RISC)

Instruction Set Architecture. or How to talk to computers if you aren t in Star Trek

Instruction Set Architecture

Typy danych. Data types: Literals:

Review: MIPS Addressing Modes/Instruction Formats

Stack machines The MIPS assembly language A simple source language Stack-machine implementation of the simple language Readings:

CSE 141 Introduction to Computer Architecture Summer Session I, Lecture 1 Introduction. Pramod V. Argade June 27, 2005

Introduction to MIPS Assembly Programming

Assembly Language Programming

MIPS Assembly Code Layout

Translating C code to MIPS

Intel 8086 architecture

Lecture Outline. Stack machines The MIPS assembly language. Code Generation (I)

More MIPS: Recursion. Computer Science 104 Lecture 9

A single register, called the accumulator, stores the. operand before the operation, and stores the result. Add y # add y from memory to the acc

Computer Systems Architecture

Winter 2002 MID-SESSION TEST Friday, March 1 6:30 to 8:00pm

Instruction Set Architecture (ISA)

Instruction Set Design

Figure 1: Graphical example of a mergesort 1.

An Introduction to Assembly Programming with the ARM 32-bit Processor Family

MIPS Assembler and Simulator

Computer organization

Exceptions in MIPS. know the exception mechanism in MIPS be able to write a simple exception handler for a MIPS machine

What to do when I have a load/store instruction?

Computer Organization and Architecture

Lab Work 2. MIPS assembly and introduction to PCSpim

COMP 303 MIPS Processor Design Project 4: MIPS Processor Due Date: 11 December :59

EC 362 Problem Set #2

Chapter 2 Topics. 2.1 Classification of Computers & Instructions 2.2 Classes of Instruction Sets 2.3 Informal Description of Simple RISC Computer, SRC

2) Write in detail the issues in the design of code generator.

M A S S A C H U S E T T S I N S T I T U T E O F T E C H N O L O G Y DEPARTMENT OF ELECTRICAL ENGINEERING AND COMPUTER SCIENCE

Computer Architecture Lecture 2: Instruction Set Principles (Appendix A) Chih Wei Liu 劉 志 尉 National Chiao Tung University

LC-3 Assembly Language

Syscall 5. Erik Jonsson School of Engineering and Computer Science. The University of Texas at Dallas

Advanced Computer Architecture-CS501. Computer Systems Design and Architecture 2.1, 2.2, 3.2

PROBLEMS (Cap. 4 - Istruzioni macchina)

Chapter 7 Assembly Language

Notes on Assembly Language

Central Processing Unit Simulation Version v2.5 (July 2005) Charles André University Nice-Sophia Antipolis

THUMB Instruction Set

a storage location directly on the CPU, used for temporary storage of small amounts of data during processing.

An Introduction to the ARM 7 Architecture

Design of Pipelined MIPS Processor. Sept. 24 & 26, 1997

MICROPROCESSOR AND MICROCOMPUTER BASICS

5 MIPS Assembly Language

Instruction Set Architecture (ISA) Design. Classification Categories

Execution Cycle. Pipelining. IF and ID Stages. Simple MIPS Instruction Formats

The AVR Microcontroller and C Compiler Co-Design Dr. Gaute Myklebust ATMEL Corporation ATMEL Development Center, Trondheim, Norway

CPU Organization and Assembly Language

8051 MICROCONTROLLER COURSE

Comp 255Q - 1M: Computer Organization Lab #3 - Machine Language Programs for the PDP-8

Instruction Set Architecture

Computer Organization and Components

Lecture 3 Addressing Modes, Instruction Samples, Machine Code, Instruction Execution Cycle

Design of Digital Circuits (SS16)

MICROPROCESSOR. Exclusive for IACE Students iacehyd.blogspot.in Ph: /422 Page 1

EE361: Digital Computer Organization Course Syllabus

what operations can it perform? how does it perform them? on what kind of data? where are instructions and data stored?

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

X86-64 Architecture Guide

CS352H: Computer Systems Architecture

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

Pipeline Hazards. Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. Based on the material prepared by Arvind and Krste Asanovic

Chapter 2 Assemblers

A SystemC Transaction Level Model for the MIPS R3000 Processor

Compilers I - Chapter 4: Generating Better Code

M6800. Assembly Language Programming

PCSpim Tutorial. Nathan Goulding-Hotta v0.1

Chapter 7D The Java Virtual Machine

Computer Organization and Components

umps software development

Laboratorio di Sistemi Operativi Anno Accademico

UNIVERSITY OF CALIFORNIA, DAVIS Department of Electrical and Computer Engineering. EEC180B Lab 7: MISP Processor Design Spring 1995

1 Classical Universal Computer 3

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 20: Stack Frames 7 March 08

Introducción. Diseño de sistemas digitales.1

HC12 Assembly Language Programming

İSTANBUL AYDIN UNIVERSITY

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

Keil C51 Cross Compiler

Addressing The problem. When & Where do we encounter Data? The concept of addressing data' in computations. The implications for our machine design(s)

PART B QUESTIONS AND ANSWERS UNIT I

The Operating System and the Kernel

The Little Man Computer

Summary of the MARIE Assembly Language

How To Write An Array In A Microsoft Zil

In this Chapter you ll learn:

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

Traditional IBM Mainframe Operating Principles

MACHINE INSTRUCTIONS AND PROGRAMS

MACHINE ARCHITECTURE & LANGUAGE

UT69R000 MicroController Software Tools Product Brief

Machine-Code Generation for Functions

MIPS Assembly Language Programming CS50 Discussion and Project Book. Daniel J. Ellard

Chapter 5 Instructor's Manual

Reusable Application-Dependent Machine Descriptions

Q. Consider a dynamic instruction execution (an execution trace, in other words) that consists of repeats of code in this pattern:

Introduction. What is an Operating System?

Transcription:

Chapter 3 MIPS Assembly Language 1

Review MIPS instruction:fixed instruction size(32bit) and 3 simple formats bne or beq: R type or I-type? Using slt and beq to simulate branch if less than pseudo instruction 2

Compiling a While Loop C Source Code: assuming that I,j,k corresponds to $s3,$s4,$s5 and the base address of the array save is in $s6 (How is this example different from the previous ones?) MIPS assembly code: while (save[i] == k) i=i+j; Loop: add $t1,$s3,$s3 #reg $t1 = 2*i add $t1,$t1,$t1 #reg $t1 = 4*i add $t1,$t1,$s6 #$t1 = address of save[i] lw $t0,0($t1) #$t0 = save[i] bne $t0,$s5, Exit # goto Exit if save[i]!=k add $s3,$s3,$s4 # i= i + j j Loop # goto Loop Exit: 3

Another Example See page 126 of text. The loop modifies I, we must multiply its value by 4 each time through the loop. Exists a more efficient method (See section 3.11, the pointer version) 4

Case/Switch Statement C source code: switch(k){ case 0: f=i+j;break; case 1: f=g+h;break; case 2: f=g-h;break; case 3: f=i-j;break; } What is the MIPS assembly code assuming f-k correspond to registers $s0-$s5 and $t2 contains 4 and $t4 contains base address of JumpTable? 5

MIPS Assembly Code for Case/Switch Slt $t3,$s5,$zero # test if k<0 bne $t3,$zero,exit # go to Exit if k<0 slt $t3,$s5,$t2 # test if k<4 beq $t3,$zero,exit # go to Exit if k>=4 add $t1,$s5,$s5 #$t1 =2*k add $t1,$t1,$t1 #$t1 =4*k add $t1,$t1,$t4 #$t1=address of JumpTable[k] lw $t0,0($t1) #$to=jumptable[k] jr $t0 #jump based on register $t0 L0: add $s0,$s3,$s4 j Exit L1: add $s0,$s1,$s2 j Exit L2: sub $s0,$s1,$s2 j Exit L3: sub $s0,$s3,$s4 Exit: 6

Supporting Procedures Basic steps: Place parameters in a place where the procedure can access them Transfer control to the procedure Acquire the storage resources needed for the procedure Perform desired task Place the result in a place where the calling program can access it Return control to the point of origin 7

Registers for Procedure Calling $a0-$a3: four argument registers $v0-$v1: two value registers $ra: return address register jump-and-link: jal ProcedureAddress jal instruction actually saves PC+4 in the register $ra return jump: jr $ra 8

Using more registers What if more than four arguments and two return values are needed? Spill register to memory use a stack data structure (last-in-first-out) to do this that s why there is another register called $sp (stack pointer) Example on page 134 of text shows how a procedure call works. Nested procedures. 9

Procedure Frame The segment of the stack containing a procedure s saved registers and local variables is called a procedure frame. Some MIPS software uses a frame pointer ($fp) to point to the first word of the frame of a procedure. (more stable) High address $fp $fp $sp $sp $fp Saved argument registers (if any) Saved return address Saved saved registers (if any) Local arrays and structures (if any) $sp Low address a. b. c. 10

MIPS Register Convention Name Reg# Usage Preserved on call? $zero 0 the constant value 0 n.a. $v0-$v1 2-3 values for results no $a0-$a3 4-7 arguments yes $t0-$t7 8-15 temporaries no $s0-$s7 16-23 saved yes $t8-$t9 24-25 more temporaries no $gp 28 global pointer yes $sp 29 stack pointer yes $fp 30 frame pointer yes $ra 31 return address yes Register 1, $at is reserved for assembler, registers 26-27, called $k0-$k1, is reserved for the operating system. 11

Beyond Numbers ASCII code Loading and saving bytes: lb $t0,0($sp) # Read byte from source sb $t0,0($gp) # Write byte to dest. Example: strcpy (page 143) 12

Addresses in Branches and Jumps Instructions: bne $t4,$t5,label Next instruction is at Label if $t4!= $t5 beq $t4,$t5,label Next instruction is at Label if $t4 = $t5 j Label Next instruction is at Label Formats: I J op rs rt 16 bit address op 26 bit address Addresses are not 32 bits How do we handle this with load and store instructions? 13

Addresses in Branches Instructions: bne $t4,$t5,label beq $t4,$t5,label Formats: Next instruction is at Label if $t4!=$t5 Next instruction is at Label if $t4=$t5 I op rs rt 16 bit address Could specify a register (like lw and sw) and add it to address use Instruction Address Register (PC = program counter) most branches are local (principle of locality) Jump instructions just use upper 4 bits of PC address boundaries of 256 MB 14

To summarize: MIPS operands Name Example Comments $s0-$s7, $t0-$t9, $zero, Fast locations for data. In MIPS, data must be in registers to perform 32 registers $a0-$a3, $v0-$v1, $gp, arithmetic. MIPS register $zero always equals 0. Register $at is $fp, $sp, $ra, $at reserved for the assembler to handle large constants. Memory[0], Accessed only by data transfer instructions. MIPS uses byte addresses, so 2 30 memory Memory[4],..., sequential words differ by 4. Memory holds data structures, such as arrays, words Memory[4294967292] and spilled registers, such as those saved on procedure calls. MIPS assembly language Category Instruction Example Meaning Comments add add $s1, $s2, $s3 $s1 = $s2 + $s3 Three operands; data in registers Arithmetic subtract sub $s1, $s2, $s3 $s1 = $s2 - $s3 Three operands; data in registers add immediate addi $s1, $s2, 100 $s1 = $s2 + 100 Used to add constants load w ord lw $s1, 100($s2) $s1 = Memory[$s2 + 100 Word from memory to register store w ord sw $s1, 100($s2) Memory[$s2 + 100] = $s1 Word from register to memory Data transfer load byte lb $s1, 100($s2) $s1 = Memory[$s2 + 100 Byte from memory to register store byte sb $s1, 100($s2) Memory[$s2 + 100] = $s1 Byte from register to memory load upper immediate lui $s1, 100 $s1 = 100 * 2 16 Loads constant in upper 16 bits branch on equal beq $s1, $s2, 25 if ($s1 == $s2) go to PC + 4 + 100 branch on not equal bne $s1, $s2, 25 if ($s1!= $s2) go to Conditional PC + 4 + 100 branch set on less than slt $s1, $s2, $s3 if ($s2 < $s3) $s1 = 1; else $s1 = 0 set less than immediate slti $s1, $s2, 100 if ($s2 < 100) $s1 = 1; else $s1 = 0 Equal test; PC-relative branch Not equal test; PC-relative Compare less than; for beq, bne Compare less than constant jump j 2500 go to 10000 Jump to target address Uncondi- jump register jr $ra go to $ra For sw itch, procedure return tional jump jump and link jal 2500 $ra = PC + 4; go to 10000 For procedure call 15

MIPS Addressing Mode Register addressing: operand is a register Base or displacement addressing: example: lw $t0,1200($t1) Immediate addressing: addi PC-relative addressing: address is the sum of the PC and a constant in the instruction (conditional branch) Pseudodirect addressing: the jump address is the 26 bits of the instruction concatenated with the upper bits of the PC (jump) 16

MIPS Addressing Mode (Cont d) 1. Immediate addressing op rs rt Immediate 2. Register addressing op rs rt rd... funct Registers Register 3. Base addressing op rs rt Address Memory Register + Byte Halfword Word 4. PC-relative addressing op rs rt Address Memory PC + Word 5. Pseudodirect addressing op Address Memory PC Word 17

Decoding Machine Code With the help of Figure 3.18, you should be able to decode the following code: 0000 0000 1010 1111 1000 0000 0010 0000 add $s0,$a1,$t7 18

Starting a Program C program Compiler Assembly language program Assembler Object: Machine language module Object: Library routine (machine language) Linker Executable: Machine language program Loader Memory 19

Using PCSPIM For Windows Messages: SPIM messages Text Segments (instruction) Data Segments: displays the data load to the program s memory and data on the program stack. Registers Console 20