MIPS ProgramTemplate

Similar documents
MIPS and QTSPIM Recap. MIPS Architecture. Cptr280. Dr Curtis Nelson. Cptr280, Autumn

MIPS Processor Overview

Instruction encoding. MIPS Instruction encoding

QtSPIM and MARS : MIPS Simulators

Memory. CS 447, Spring /25/16. Memory. Processor (CPU) transfer

Slide Set 3. for ENCM 369 Winter 2018 Section 01. Steve Norman, PhD, PEng

CSCI 402: Computer Architectures

Processor design - MIPS

Programming in MIPS. Tutorial

CS3350B Computer Architecture MIPS Introduction

Sparse Notes on an MIPS Processor s Architecture and its Assembly Language

Course Administration

Arithmetic for Computers

2.1 DOWNLOAD AND INSTALL MARS

Operations, Operands, and Instructions

Review of the Machine Cycle

CS3350B Computer Architecture MIPS Instruction Representation

CS3350B Computer Architecture

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

Floating Point. What can be represented in N bits? 0 to 2N-1. 9,349,398,989,787,762,244,859,087, x 1067

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

Computer Architecture

EECS Computer Organization Fall Based on slides by the author and prof. Mary Jane Irwin of PSU.

MIPS (SPIM) Assembler Syntax

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

MIPS Assembly Language

ICS 233 Computer Architecture & Assembly Language. ICS 233 Computer Architecture & Assembly Language

Five classic components

Instructions: Language of the Computer

MIPS Hello World. MIPS Assembly 1. # PROGRAM: Hello, World! # Data declaration section. out_string:.asciiz "\nhello, World!\n"

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

MIPS Assembly Programming

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Computer Science and Engineering 331. Midterm Examination #1. Fall Name: Solutions S.S.#:

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

(Refer Slide Time: 1:40)

Lecture 7: Examples, MARS, Arithmetic

MIPS Instruction Set

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

17. Instruction Sets: Characteristics and Functions

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

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

Computer Science 61C Spring Friedland and Weaver. Instruction Encoding

USING A SIMULATOR. QUICK INTRODUCTION From various sources For cs470

ECE 154A Introduction to. Fall 2012

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

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

Contents. Slide Set 1. About these slides. Outline of Slide Set 1. Typographical conventions: Italics. Typographical conventions. About these slides

Introduction to SPIM Programming

EN164: Design of Computing Systems Lecture 09: Processor / ISA 2

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

CS2214 COMPUTER ARCHITECTURE & ORGANIZATION SPRING 2014

MIPS Memory Access Instructions

MIPS Reference Guide

COMP MIPS instructions 2 Feb. 8, f = g + h i;

CPS311 - COMPUTER ORGANIZATION. A bit of history

ECE232: Hardware Organization and Design

COMP2421 COMPUTER ORGANZATION. Lab 3

Lecture #6 Intro MIPS; Load & Store

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Lectures 3-4: MIPS instructions

Number Systems and Their Representations

Slide Set 1 (corrected)

Computer Organization Lab #3.

Shift and Rotate Instructions

Grading: 3 pts each part. If answer is correct but uses more instructions, 1 pt off. Wrong answer 3pts off.

Kernel Registers 0 1. Global Data Pointer. Stack Pointer. Frame Pointer. Return Address.

MIPS Coding Continued

CS 61c: Great Ideas in Computer Architecture

MIPS Assembly Language Programming

EC 413 Computer Organization

Computer Organization MIPS ISA

Instructions: Language of the Computer

Programmable Machines

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

Programmable Machines

Reduced Instruction Set Computer (RISC)

CSEE 3827: Fundamentals of Computer Systems

Overview. Introduction to the MIPS ISA. MIPS ISA Overview. Overview (2)

MIPS Assembly: More about Memory and Instructions CS 64: Computer Organization and Design Logic Lecture #7

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

CS 265. Computer Architecture. Wei Lu, Ph.D., P.Eng.

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

Chapter 2. Instruction Set. RISC vs. CISC Instruction set. The University of Adelaide, School of Computer Science 18 September 2017

Computer Architecture. MIPS Instruction Set Architecture

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

CS61C - Machine Structures. Lecture 6 - Instruction Representation. September 15, 2000 David Patterson.

MIPS Assembly Language Programming

CS222: MIPS Instruction Set

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

MIPS Assembly Language

LAB C Translating Utility Classes

instructions aligned is little-endian

Topic Notes: MIPS Instruction Set Architecture

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

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

Anne Bracy CS 3410 Computer Science Cornell University. See P&H Chapter: , , Appendix B

Chapter 2A Instructions: Language of the Computer

Reduced Instruction Set Computer (RISC)

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Transcription:

MIPS Load and Store Instructions Cptr280 Dr Curtis Nelson MIPS ProgramTemplate # Comment giving name of program and description of function # Template.s # Bare-bones outline of MIPS assembly language program.data # Variable declarations here # Starts at address 0x10010000.text.globl main # starts at address 0x00400000 main: # indicates start of code (first instruction to execute) # remainder of program code here #... #... 1

MIPS Assembler Directives Top-level Directives:.text Indicates that following items are stored in the user text segment, typically instructions..data Indicates that following data items are stored in the data segment..globl sym Declare that symbol sym is global and can be referenced from other files. Pseudo-instructions The MIPS assembler supports several pseudo-instructions: Not directly supported in hardware; Implemented using one or more supported instructions; Simplify assembly language programming and translation. For example, the pseudo-instruction la $t0, label is implemented as lui $8, 4097 [label] (we will discuss this instruction shortly) The pseudo-instruction move $t0, $t1 is implemented as add $t0, $zero, $t1 (because register 0 is always = 0) The pseudo-instruction li $t4, 20 is implemented as ori $t4, $0, 20 See the link QTSpim Quick Reference on the course web page for a list of the MIPS instructions and pseudo-instructions (macros) supported by the QTSpim assembler. 2

Load and Store The operands for all arithmetic and logic operations are contained in registers. To operate on data in main memory, the data is first copied into registers. A load operation copies data from main memory into a register. A store operation copies data from a register into main memory. When a word (4-bytes) is loaded or stored, the memory address must be a multiple of four. This is called an alignment restriction. Addresses that are a multiple of four are called word aligned. This restriction makes the hardware simpler and faster. The lw instruction loads a word into a register from memory. The sw instruction stores a word from a register into memory. Each instruction specifies a register and a memory address (details in a few slides). Quiz Which of the following addresses are word aligned? 0x000AE430 0x00014432 0x000B0737 0x0E0D8844 3

Big Endian and Little Endian A load word or store word instruction uses only one memory address. The lowest address of the four bytes is used for the address of a block of four contiguous bytes. How is a 32-bit pattern held in the four bytes of memory? There are 32 bits in the four bytes and 32 bits in the pattern, but a choice has to be made about which byte of memory gets what part of the pattern. There are two ways that computers commonly do this: Big Endian Byte Order: The most significant byte (the "big end") of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory. Little Endian Byte Order: The least significant byte (the "little end") of the data is placed at the byte with the lowest address. The rest of the data is placed in order in the next three bytes in memory. Big Endian and Little Endian In these definitions, the data, a 32-bit pattern, is regarded as a 32-bit unsigned integer. The "most significant" byte is the one for the largest powers of two: 2 31,..., 2 24. The "least significant" byte is the one for the smallest powers of two: 2 7,..., 2 0. For example, say that the 32-bit pattern 0x12345678 is at address 0x00400000. The most significant byte is 0x12; the least significant is 0x78. Here are the two byte orders: Within a byte, the order of the bits is the same for all computers (no matter how the bytes themselves are arranged). 4

Byte Order of MIPS and QTSpim Within a byte, for all processors, bit 7 is the most significant bit. So the big end byte looks the same for both byte orderings. Usually in printed material this bit is shown at the left, as in 00010010. Note: except when discussing byte ordering, the "big end" byte is called the "highorder byte" or the "most significant byte". The MIPS processor chip can be set up in hardware to use either byte ordering. A computer system designer makes whatever choice best fits the rest of the components in the computer system. The QTSpim simulator uses the byte ordering of the computer it is running on. Intel 80x86: little-endian. Portability Problems When a word is loaded from memory, the electronics puts the bytes into the register in the correct order. Operations (such as addition) inside the processor use the same order. When the register is stored to memory the bytes are written in the same order. As long as the electronics is consistent, either byte order works. Usually you don't need to think about which order is used. However, when data from one computer is used on another you do need to be concerned. Say that you have a file of integer data that was written by an old mainframe computer. To read it correctly, you need to know (among other things): The number of bits used to represent each integer; The representational scheme used to represent integers (two's complement or other); Which byte ordering (little or big endian) was used. 5

MIPS Addresses The MIPS instruction that loads a word into a register is the lw instruction. The store word instruction is sw. Each must specify a register and a memory address. A MIPS instruction is 32 bits (always). A MIPS memory address is 32 bits (always). How can a load or store instruction specify an address that is the same size as itself? An instruction that refers to memory uses a base register and an offset. The base register is a general purpose register that contains a 32-bit address. The offset is a 16-bit signed integer contained in the instruction. The sum of the address in the base register with the (sign-extended) offset forms the memory address. Here is the load word instruction in assembly language: lw d,offset(b) # $d ß Word from memory address (b + offset) # b is a register, offset is 16-bit two's complement. At execution time two things happen: (1) an address is calculated using the base register b and the offset offset, and (2) data is fetched from memory at that address. Question Write the instruction that loads the word at address 0x00400060 into register $8. Assume that register $10 contains 0x00400000. lw $8, ( ) 6

Machine Instruction for Load Word Here is the machine code version of the instruction. It specifies the base register, the destination register, and the offset. It does not directly contain the memory address: 100011 01010 01000 0000 0000 0110 0000 -- lw $8,0x60($10) lw $10 $8 0 0 6 0 opcode base dest offset -- meaning of the fields Here is how this instruction is executed: 1. The 32-bit address in $10 is: 0x00400000 2. The offset is sign-extended to 32 bits: 0x00000060 3. The memory address is the 32-bit sum of the above: 0x00400060 4. Main memory is asked for data from that address. 5. After a one machine cycle delay the data reaches $8. $8 ß 4 bytes from location 0x00400060 There is a one machine cycle delay before the data from memory is available. Reaching outside of the processor chip into main memory takes time. But the processor does not wait and executes one more instruction while the load is going on. This is the load delay slot. The instruction immediately after a lw instruction should not use the register that is being loaded. Sometimes the instruction after the lw is a no-operation instruction. Store Word Instruction The store word instruction, sw, copies data from a register to memory. The register is not changed. The memory address is specified using a base/register pair. sw $t,offset(b) # Word at memory address (b+offset) ß $t # b is a register, offset is 16-bit two's complement As with the lw instruction, the memory address must be word aligned (a multiple of four). There are similar instructions to load and store bytes (8 bits) and halfwords (16 bits). 7

Example: add2numbersprog2.asm # Program adds 10 and 20.text.globl main # text section # call main by SPIM main: la $t0, value # load address value into $t0 lw $t1, 0($t0) # load word 0 (value) into $t1 lw $t2, 4($t0) # load word 4 (value) into $t2 add $t3, $t1, $t2 # add two numbers into $t3 sw $t3, 8($t0) # store word $t3 into 8 ($t0).data # data section value:.word 10, 20, 0 # load data integers # Default data start address is 0x10010000 # This is where (value) is stored. Example: swap2memorywords.asm ## Program to swap two memory words.data.word 7.word 3 # load data.text.globl main main: lui $s0, 0x1001 # load data area start address 0x10010000 lw $s1, 0($s0) lw $s2, 4($s0) sw $s2, 0($s0) sw $s1, 4($s0) 8

Example: storewords.asm ## Program shows memory storage and access (big vs. little endian).data here:.word 0xabc89725, 100.byte 0, 1, 2, 3.asciiz "Sample text" there:.space 6.byte 85.align 2.byte 32 main:.text.globl main la $t0, here lbu $t1, 0($t0) lbu $t2, 1($t0) lw $t3, 0($t0) sw $t3, 36($t0) sb $t3, 41($t0) SPIM s memory storage depends on the underlying machine: Intel 80x86 processors are little-endian. Word placement in memory is exactly the same in big or little endian a copy is placed. Byte placement in memory depends on if it is big or little endian. In big-endian, bytes in a Word are counted from the byte 0 at the left (most significant) to byte 3 at the right (least significant); in little-endian it is the other way around. Word access (lw, sw) is exactly the same in big or little endian it is a copy from register to a memory word or vice versa. Byte access depends on if it is big or little endian, because bytes are counted 0 to 3 from left to right in big-endian and counted 0 to 3 from right to left in little-endian. More SPIM Example Programs In the class examples directory you will find 18 simple well-documented MIPS assembly programs. Run the code in the order below of increasing complexity. 1. add2numbersprog1 2. add2numbersprog2 3. storewords 4. swap2memorywords 5. branchjump 6. systemcalls 7. overflow 8. averageofbytes 9. printloop 10. sumofsquaresprog1 11. sumofsquaresprog2 12. sumofsquaresprog3 13. proccallsprog1 14. proccallsprog1modified 15. proccallsprog2 16. addfirst100 17. factorialnonrecursive 18. factorialrecursive 9

Summary Memory usage (memory map); Pseudo instructions; Load and store instructions. 10