Introduction to MIPS Assembly Programming



Similar documents
Typy danych. Data types: Literals:

Reduced Instruction Set Computer (RISC)

MIPS Assembly Code Layout

Assembly Language Programming

5 MIPS Assembly Language

Instruction Set Architecture

MIPS Assembler and Simulator

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

Translating C code to MIPS

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

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

Review: MIPS Addressing Modes/Instruction Formats

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

Lab Work 2. MIPS assembly and introduction to PCSpim

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

Figure 1: Graphical example of a mergesort 1.

Computer Systems Architecture

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

Lecture 8: Binary Multiplication & Division

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

SMIPS Processor Specification

Lecture 22: C Programming 4 Embedded Systems

612 CHAPTER 11 PROCESSOR FAMILIES (Corrisponde al cap Famiglie di processori) PROBLEMS

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

Chapter 7D The Java Virtual Machine

Introduction to MIPS Programming with Mars

More MIPS: Recursion. Computer Science 104 Lecture 9

EE361: Digital Computer Organization Course Syllabus

CPU Organization and Assembly Language

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

THUMB Instruction Set

Instruction Set Reference

Computer Science 281 Binary and Hexadecimal Review

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

umps software development

How To Write An Array In A Microsoft Zil

Instruction Set Design

1. Convert the following base 10 numbers into 8-bit 2 s complement notation 0, -1, -12

The programming language C. sws1 1

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

Chapter 5 Instructor's Manual

Sources: On the Web: Slides will be available on:

Informatica e Sistemi in Tempo Reale

How It All Works. Other M68000 Updates. Basic Control Signals. Basic Control Signals

Intel 8086 architecture

A SystemC Transaction Level Model for the MIPS R3000 Processor

EECS 427 RISC PROCESSOR

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

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

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

Chapter 2: Algorithm Discovery and Design. Invitation to Computer Science, C++ Version, Third Edition

EMC Publishing. Ontario Curriculum Computer and Information Science Grade 11

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

Instruction Set Architecture (ISA) Design. Classification Categories

Efficient Low-Level Software Development for the i.mx Platform

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

ELEG3924 Microprocessor Ch.7 Programming In C

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

Caml Virtual Machine File & data formats Document version: 1.4

Numeral Systems. The number twenty-five can be represented in many ways: Decimal system (base 10): 25 Roman numerals:

1/20/2016 INTRODUCTION

Divide: Paper & Pencil. Computer Architecture ALU Design : Division and Floating Point. Divide algorithm. DIVIDE HARDWARE Version 1

Introduction to the Altera Nios II Soft Processor. 1 Introduction. For Quartus II 11.1

OAMulator. Online One Address Machine emulator and OAMPL compiler.

Instruction Set Architecture (ISA)

Instruction Set Architecture

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

Glossary of Object Oriented Terms

ALLIED PAPER : DISCRETE MATHEMATICS (for B.Sc. Computer Technology & B.Sc. Multimedia and Web Technology)

İSTANBUL AYDIN UNIVERSITY

Memory Systems. Static Random Access Memory (SRAM) Cell

Computer Architecture Lecture 3: ISA Tradeoffs. Prof. Onur Mutlu Carnegie Mellon University Spring 2013, 1/18/2013

System i Architecture Part 1. Module 2

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

MACHINE ARCHITECTURE & LANGUAGE

Machine-Code Generation for Functions

Programing the Microprocessor in C Microprocessor System Design and Interfacing ECE 362

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

CS 40 Computing for the Web

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

LC-3 Assembly Language

Chapter 6: Programming Languages

X86-64 Architecture Guide

Useful Number Systems

Moving from CS 61A Scheme to CS 61B Java

COMPUTER ORGANIZATION ARCHITECTURES FOR EMBEDDED COMPUTING

Keil C51 Cross Compiler

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

1 Classical Universal Computer 3

AN617. Fixed Point Routines FIXED POINT ARITHMETIC INTRODUCTION. Thi d t t d ith F M k Design Consultant

High level code and machine code

Computer Organization and Architecture

CS101 Lecture 26: Low Level Programming. John Magee 30 July 2013 Some material copyright Jones and Bartlett. Overview/Questions

Faculty of Engineering Student Number:

Computer Organization and Components

LSN 2 Computer Processors

Streaming Lossless Data Compression Algorithm (SLDC)

Transcription:

1 / 26 Introduction to MIPS Assembly Programming January 23 25, 2013

2 / 26 Outline Overview of assembly programming MARS tutorial MIPS assembly syntax Role of pseudocode Some simple instructions Integer logic and arithmetic Manipulating register values Interacting with data memory Declaring constants and variables Reading and writing Performing input and output Memory-mapped I/O, role of the OS Using the systemcall interface

3 / 26 Assembly program template # Author: your name # Date: current date # Description: high-level description of your program.data Data segment: constant and variable definitions go here.text Text segment: assembly instructions go here

4 / 26 Components of an assembly program Lexical category Example(s) Comment # do the thing Assembler directive.data,.asciiz,.global Operation mnemonic add, addi, lw, bne Register name $10, $t2 Address label (decl) hello:, length:, loop: Address label (use) hello, length, loop Integer constant 16, -8, 0xA4 String constant "Hello, world!\n" Character constant H,?, \n

5 / 26 Lexical categories in hello world # Author: Eric Walkingshaw # Date: Jan 18, 2013 # Description: A simple hello world program!.data hello:.text # add this stuff to the data segment # load the hello string into data memory.asciiz "Hello, world!" # now we re in the text segment li $v0, 4 # set up print string syscall la $a0, hello # argument to print string syscall # tell the OS to do the syscall li $v0, 10 # set up exit syscall syscall # tell the OS to do the syscall

6 / 26 Pseudocode What is pseudocode? informal language intended to be read by humans Useful in two different roles in this class: 1. for understanding assembly instructions 2. for describing algorithms to translate into assembly Example of role 1: lw $t1, 8($t2) Pseudocode: $t1 = Memory[$t2+8] Pseudocode is not real code! Just a way to help understand what an operation does

7 / 26 How to write assembly code Writing assembly can be overwhelming and confusing Strategy 1. develop algorithm in pseudocode 2. break it into small pieces 3. implement (and test) each piece in assembly It is extremely helpful to annotate your assembly code with the pseudocode it implements! helps to understand your code later much easier to check that code does what you intended

8 / 26 Outline Overview of assembly programming MARS tutorial MIPS assembly syntax Role of pseudocode Some simple instructions Integer logic and arithmetic Manipulating register values Interacting with data memory Declaring constants and variables Reading and writing Performing input and output Memory-mapped I/O, role of the OS Using the systemcall interface

9 / 26 MIPS register names and conventions Number Name Usage Preserved? $0 $zero constant 0x00000000 N/A $1 $at assembler temporary $2 $3 $v0 $v1 function return values $4 $7 $a0 $a3 function arguments $8 $15 $t0 $t7 temporaries $16 $23 $s0 $s7 saved temporaries $24 $25 $t8 $t9 more temporaries $26 $27 $k0 $k1 reserved for OS kernel N/A $28 $gp global pointer $29 $sp stack pointer $30 $fp frame pointer $31 $ra return address (for reference)

Integer logic and arithmetic # Instruction # Meaning in pseudocode add $t1, $t2, $t3 # $t1 = $t2 + $t3 sub $t1, $t2, $t3 # $t1 = $t2 - $t3 and $t1, $t2, $t3 # $t1 = $t2 & $t3 (bitwise and) or $t1, $t2, $t3 # $t1 = $t2 $t3 (bitwise or) # set if equal: seq $t1, $t2, $t3 # $t1 = $t2 == $t3? 1 : 0 # set if less than: slt $t1, $t2, $t3 # $t1 = $t2 < $t3? 1 : 0 # set if less than or equal: sle $t1, $t2, $t3 # $t1 = $t2 <= $t3? 1 : 0 Some other instructions of the same form xor, nor sne, sgt, sge 10 / 26

11 / 26 Immediate instructions Like previous instructions, but second operand is a constant constant is 16-bits, sign-extended to 32-bits (reason for this will be clear later) # Instruction # Meaning in pseudocode # add/subtract/and immediate: addi $t1, $t2, 4 # $t1 = $t2 + 4 subi $t1, $t2, 15 # $t1 = $t2-15 andi $t1, $t2, 0x00FF # $t1 = $t2 & 0x00FF # set if less than immediate: slti $t1, $t2, 42 # $t1 = $t2 < 42? 1 : 0 Some other instructions of the same form ori, xori

12 / 26 Multiplication Result of multiplication is a 64-bit number stored in two 32-bit registers, hi and lo # Instruction # Meaning in pseudocode mult $t1, $t2 # hi,lo = $t1 * $t2 mflo $t0 # $t0 = lo mfhi $t3 # $t3 = hi Shortcut (macro instruction): mul $t0, $t1, $t2 # hi,lo = $t1 * $t2; $t0 = lo Expands to: mult $t1, $t2 mflo $t0

13 / 26 Integer division Computes quotient and remainder and simultaneously stores quotient in lo, remainder in hi # Instruction # Meaning in pseudocode div $t1, $t2 # lo = $t1 / $t2; hi = $t1 % $t2

14 / 26 Manipulating register values # Instruction # Meaning in pseudocode # copy register: move $t1, $t2 # $t1 = $t2 # load immediate: load constant into register (16-bit) li $t1, 42 # $t1 = 42 li $t1, k # $t1 = 0x6B # load address into register la $t1, label # $t1 = label

15 / 26 Outline Overview of assembly programming MARS tutorial MIPS assembly syntax Role of pseudocode Some simple instructions Integer logic and arithmetic Manipulating register values Interacting with data memory Declaring constants and variables Reading and writing Performing input and output Memory-mapped I/O, role of the OS Using the systemcall interface

16 / 26 Declaring constants and variables Parts of a declaration: (in data segment) 1. label: memory address of variable 2. directive: type of data (used by assembler when initializing memory, not enforced) 3. constant: the initial value.data # string prompt constant prompt:.asciiz "What is your favorite number?: " # variable to store response favnum:.word 0 No real difference between constants and variables! All just memory we can read and write

17 / 26 Sequential declarations Sequential declarations will be loaded sequentially in memory we can take advantage of this fact Example 1: Splitting long strings over multiple lines # help text help:.ascii "The best tool ever. (v.1.0)\n".ascii "Options:\n".asciiz " --h Print this help text.\n" Example 2: Initializing an array of data fibs:.word 0, 1, 1, 2, 3, 5, 8, 13, 21, 35, 55, 89, 144

18 / 26 Reserving space Reserve space in the data segment with the.space directive argument is number of bytes to reserve useful for arrays of data we don t know in advance Example: Reserve space for a ten integer array array:.space 40 array is the address of the 0th element of the array address of other elements: array+4, array+8, array+12,..., array+36 (MARS demo: Decls.asm)

19 / 26 Reading from data memory Basic instruction for reading data memory ( load word ): lw $t1, 4($t2) # $t1 = Memory[$t2+4] $t2 contains the base address 4 is the offset lw $t1, $t2 lw $t1, 0($t2) Macro instructions to make reading memory at labels nice: lw $t1, label # $t1 = Memory[label] lw $t1, label + 4 # $t1 = Memory[label+4]

20 / 26 Writing to data memory Basic instruction for writing to memory ( store word ): sw $t1, 4($t2) # Memory[$t2+4] = $t1 $t2 contains the base address 4 is the offset sw $t1, $t2 sw $t1, 0($t2) Macro instructions to make writing memory at labels nice: sw $t1, label # Memory[label] = $t1 sw $t1, label + 4 # Memory[label+4] = $t1 (MARS demo: Add3.asm)

21 / 26 Sub-word addressing Reading sub-word data lb: load byte (sign extend) lh: load halfword (sign extend) lbu: load byte unsigned (zero extend) lhu: load halfword unsigned (zero extend) Remember, little-endian addressing: 3 2 1 0 7 6 5 4 11 10 9 8 15 14 13 12 (MARS demo: SubWord.asm)

22 / 26 Reading/writing data memory wrap up Writing sub-word data sb: store byte (low order) sh: store halfword (low order) Important: lw and sw must respect word boundaries! address (base+offset) must be divisible by 4 Likewise for lh, lhu, and sh address must be divisible by 2

23 / 26 Outline Overview of assembly programming MARS tutorial MIPS assembly syntax Role of pseudocode Some simple instructions Integer logic and arithmetic Manipulating register values Interacting with data memory Declaring constants and variables Reading and writing Performing input and output Memory-mapped I/O, role of the OS Using the systemcall interface

Memory-mapped I/O Problem: architecture must provide an interface to the world should be general (lots of potential devices) should be simple (RISC architecture) Solution: Memory-mapped I/O Memory and I/O share the same address space A range of addresses are reserved for I/O: input: load from a special address output: store to a special address So we can do I/O with just lw and sw! (at least in embedded systems) 24 / 26

25 / 26 Role of the operating system Usually, however: we don t know (or want to know) the special addresses user programs don t have permission to use them directly Operating system (kernel) knows the addresses and has access to them provides services to interact with them services are requested through system calls (the operating system does a lot more too)

26 / 26 System calls System calls are an interface for asking the OS to do stuff How system calls work, from our perspective 1. syscall hey OS, I want to do something! 2. OS checks $v0 to see what you want to do 3. OS gets arguments from $a0 $a3 (if needed) 4. OS does it 5. OS puts results in registers (if applicable) MARS help gives a list of system call services (MARS demo: Parrot.asm)