CENG3420 Lab 1-1: MIPS assembly language programing

Similar documents
Programming in MIPS. Tutorial

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

MIPS Processor Overview

MIPS Architecture and Assembly Language Overview

COMP2421 COMPUTER ORGANZATION. Lab 3

Introduction to MIPS Processor

Compiling Techniques

Course Administration

QtSPIM and MARS : MIPS Simulators

SPIM & MIPS Programming

instructions aligned is little-endian

CPSC 330 Computer Organization

MIPS Assembly Language

SPIM Instruction Set

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

MIPS Assembly (Functions)

Orange Coast College. Business Division. Computer Science Department CS 116- Computer Architecture. The Instructions

SPIM S20: A MIPS R2000 Simulator

SPIM & MIPS. Department of Information and Management. Computer Organization and Structure 年 9 月 29 日星期 一

MIPS Procedure Calls. Lecture 6 CS301

Review Session 1 Fri. Jan 19, 2:15 pm 3:05 pm Thornton 102

ECE 30 Introduction to Computer Engineering

SPIM & MIPS Programming

Lecture # 1. SPIM & MIPS Programming

MIPS function continued

Assembly labs start this week. Don t forget to submit your code at the end of your lab section. Download MARS4_5.jar to your lab PC or laptop.

COMP 303 Computer Architecture Lecture 3. Comp 303 Computer Architecture

USING A SIMULATOR. QUICK INTRODUCTION From various sources For cs470

Going From C to MIPS Assembly. Getting Started

Lecture 7: Examples, MARS, Arithmetic

MIPS Coding Snippets. Prof. James L. Frankel Harvard University. Version of 9:32 PM 14-Feb-2016 Copyright 2016 James L. Frankel. All rights reserved.

ECE 473 Computer Architecture and Organization Lab 4: MIPS Assembly Programming Due: Wednesday, Oct. 19, 2011 (30 points)

Field 6-Bit Op Code rs Field rt Field 16-bit Immediate field

MIPS Assembly Language Programming

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

Programming with QtSpim: A User s Manual

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

MIPS Assembly (FuncDons)

MIPS Assembly (FuncDons)

MIPS Architecture and Assembly Language Overview

CS3350B Computer Architecture

MIPS Assembly Language Programming

Procedure Calling. Procedure Calling. Register Usage. 25 September CSE2021 Computer Organization

CENG3420 Computer Organization and Design Lab 1-2: System calls and recursions

Graduate Institute of Electronics Engineering, NTU. SPIM and MIPS Asm. Presenter: 沈文中 (TA) Date: 2004/10/13

Computer Systems and Architecture

Run-time Environment

CS3350B Computer Architecture MIPS Introduction

EECS 322 The SPIM simulator

We can emit stack-machine-style code for expressions via recursion

ICS DEPARTMENT ICS 233 COMPUTER ARCHITECTURE & ASSEMBLY LANGUAGE. Midterm Exam. First Semester (141) Time: 1:00-3:30 PM. Student Name : _KEY

COMP2611: Computer Organization MIPS function and recursion

CSc 256 Midterm 1 Fall 2011

S Computer Architecture. Computer architecture assignment E1 A subroutine, which searches for given byte pattern from an byte array.

ECE 250 / CS 250 Computer Architecture. Procedures

MIPS Assembly Language

Lab 4 Prelab: MIPS Function Calls

Instructions: Language of the Computer

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

MIPS Assembly Language Guide

A crash course in MIPS assembly programming

Fibonacci Numbers. An Exercise in Assembly Language Programming. Andreas Klappenecker. September 7, 2004

MIPS Programming. A basic rule is: try to be mechanical (that is, don't be "tricky") when you translate high-level code into assembler code.

CA Compiler Construction

Implementing Procedure Calls

Computer Architecture

EC 413 Computer Organization

Assembly Language Programming

See P&H 2.8 and 2.12, and A.5-6. Prof. Hakim Weatherspoon CS 3410, Spring 2015 Computer Science Cornell University

Control Instructions. Computer Organization Architectures for Embedded Computing. Thursday, 26 September Summary

Control Instructions

Computer Science 2500 Computer Organization Rensselaer Polytechnic Institute Spring Topic Notes: MIPS Programming

Function Calling Conventions 1 CS 64: Computer Organization and Design Logic Lecture #9

MIPS Assembly Language. Today s Lecture

Lectures 5. Announcements: Today: Oops in Strings/pointers (example from last time) Functions in MIPS

Five classic components

Today s Lecture. MIPS Assembly Language. Review: What Must be Specified? Review: A Program. Review: MIPS Instruction Formats

MIPS Reference Guide

Code Generation. The Main Idea of Today s Lecture. We can emit stack-machine-style code for expressions via recursion. Lecture Outline.

Reduced Instruction Set Computer (RISC)

CSc 256 Midterm (green) Fall 2018

Do-While Example. In C++ In assembly language. do { z--; while (a == b); z = b; loop: addi $s2, $s2, -1 beq $s0, $s1, loop or $s2, $s1, $zero

Shift and Rotate Instructions

2.1 DOWNLOAD AND INSTALL MARS

SPIM Procedure Calls

Functions and the MIPS Calling Convention 2 CS 64: Computer Organization and Design Logic Lecture #11

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

Review (1/2) IEEE 754 Floating Point Standard: Kahan pack as much in as could get away with. CS61C - Machine Structures

4.2: MIPS function calls

Function Calling Conventions 2 CS 64: Computer Organization and Design Logic Lecture #10

Assignment 1: Pipelining Implementation at SPIM Simulator

Assembler. Lecture 8 CS301

EEC 581 Computer Architecture Lecture 1 Review MIPS

MIPS Functions and the Runtime Stack

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

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

Code Generation. Lecture 19

ECE331: Hardware Organization and Design

Math 230 Assembly Programming (AKA Computer Organization) Spring 2008

Calling Conventions. See P&H 2.8 and Hakim Weatherspoon CS 3410, Spring 2013 Computer Science Cornell University

Transcription:

CENG3420 Lab 1-1: MIPS assembly language programing Haoyu YANG Department of Computer Science and Engineering The Chinese University of Hong Kong hyyang@cse.cuhk.edu.hk 2017 Spring 1 / 18

Overview SPIM Assembly programing System service in SPIM Lab assignment 2 / 18

Overview SPIM Assembly programing System service in SPIM Lab assignment 3 / 18

What is SPIM SPIM is a MIPS32 simulator. Spim is a self-contained simulator that runs MIPS32 programs. It reads and executes assembly language programs written for this processor. Spim also provides a simple debugger and minimal set of operating system services. Spim does not execute binary (compiled) programs. Dowload it here: http://sourceforge.net/projects/ spimsimulator/files/ 3 / 18

SPIM Overview 4 / 18 What SPIM looks like.

Register Panel and Memory Panel 5 / 18 There s also a console window.

Operations Load a source file: File Reinitialize and Load File Run the code: F5 or Press the green triangle button Single stepping: F10 Breakpoint: in Text panel, right click on an address to set a breakpoint there. 6 / 18

Overview SPIM Assembly programing System service in SPIM Lab assignment 7 / 18

Registers 32 general-purpose registers register preceded by $ in assembly language instruction two formats for addressing: using register number e.g. $0 through $31 using equivalent names e.g. $t1, $sp special registers Lo and Hi used to store result of multiplication and division not directly addressable; contents accessed with special instruction mfhi ( move from Hi ) and mflo ( move from Lo ) 7 / 18

Register Names and Descriptions Name Register Number Usage Preserve on call? $zero 0 constant 0 (hardware) n.a. $at 1 reserved for assembler n.a. $v0 - $v1 2-3 returned values no $a0 - $a3 4-7 arguments yes $t0 - $t7 8-15 temporaries no $s0 - $s7 16-23 saved values yes $t8 - $t9 24-25 temporaries no $gp 28 global pointer yes $sp 29 stack pointer yes $fp 30 frame pointer yes $ra 31 return addr (hardware) yes 8 / 18

Data Types and Literals Data types: Instructions are all 32 bits byte(8 bits), halfword (2 bytes), word (4 bytes) a character requires 1 byte of storage an integer requires 1 word (4 bytes) of storage Data types:.asciiz for string,.word for int,... Literals: numbers entered as is. e.g. 4 characters enclosed in single quotes. e.g. b strings enclosed in double quotes. e.g. A string 9 / 18

Program Structure I Just plain text file with data declarations, program code (name of file should end in suffix.s to be used with SPIM simulator) Data declaration section followed by program code section Data Declarations Identified with assembler directive.data. Declares variable names used in program Storage allocated in main memory (RAM) <name>:.<datatype> <value> 10 / 18

Program Structure II Code placed in section of text identified with assembler directive.text contains program code (instructions) starting point for code e.g. execution given label main: ending point of main code should use exit system call Comments anything following # on a line 11 / 18

Program Structure III The structure of an assembly program looks like this: Program outline # Comment giving name of program and description # Template.s # Bare-bones outline of MIPS assembly language program.globl main main:.data.text # variable declarations follow this line #... # instructions follow this line # indicates start of code #... # End of program, leave a blank line afterwards 12 / 18

An Example Program li: load immediate la: load address 13 / 18

More Information For more information about MIPS instructions and assembly programing you can refer to: 1. Lecture slides and textbook. 2. http://www.mrc.uidaho.edu/mrc/people/jff/ digital/mipsir.html 14 / 18

Overview SPIM Assembly programing System service in SPIM Lab assignment 15 / 18

System calls in SPIM I SPIM provides a small set of operating system-like services through the system call (syscall) instruction. 15 / 18

System calls in SPIM II To request a service, a program loads the system call code into register $v0 and arguments into registers $a0-$a3(or $f12 for floating-point values). System calls that return values put their results in register $v0 (or $f0 for floating-point results). Like this example: Using system call.data str:.asciiz "the answer = " #labels always followed by colon.text li $v0, 4 # system call code for print_str la $a0, str # address of string to print syscall # print the string li $v0, 1 # system call code for print_int li $a0, 5 # integer to print syscall # print it 16 / 18

Overview SPIM Assembly programing System service in SPIM Lab assignment 17 / 18

Lab Assignment Write an assembly program with the following requirements: 1. Define two variables var1 and var2 which have initial value 15 and 19, respectively. 2. Print var1 and var2. 3. Print RAM addresses of var1 and var2 using syscall. 4. Swap var1 and var2 and print them. Lab report should include (1) source code, (2) console output. 17 / 18

Some Tips 1. Variables should be declared following the.data identifier. 2. <name>:.<datatype> <value> 3. Use la instruction to access the RAM address of declared data. 4. Use system call to print integers. 5. Do not forget exit system call. 18 / 18