CSE 378 Quiz Section #2 Coding in MIPS Assembly

Similar documents
MIPS Assembly Code Layout

Introduction to MIPS Assembly Programming

Lab Work 2. MIPS assembly and introduction to PCSpim

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

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

Typy danych. Data types: Literals:

Figure 1: Graphical example of a mergesort 1.

Introduction to MIPS Programming with Mars

Translating C code to MIPS

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

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

Computer Systems Architecture

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

Lab Experience 17. Programming Language Translation

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Lecture 22: C Programming 4 Embedded Systems

Java Interview Questions and Answers

Instruction Set Architecture

The programming language C. sws1 1

More MIPS: Recursion. Computer Science 104 Lecture 9

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

Moving from CS 61A Scheme to CS 61B Java

In this Chapter you ll learn:

arrays C Programming Language - Arrays

Assembly Language Programming

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

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.

Pseudo code Tutorial and Exercises Teacher s Version

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

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

Semantic Analysis: Types and Type Checking

Instruction Set Architecture (ISA)

Memory management. Announcements. Safe user input. Function pointers. Uses of function pointers. Function pointer example

Keil C51 Cross Compiler

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

13 Classes & Objects with Constructors/Destructors

Intel 8086 architecture

How To Write An Array In A Microsoft Zil

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

X86-64 Architecture Guide

C Compiler Targeting the Java Virtual Machine

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

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

AP Computer Science Java Mr. Clausen Program 9A, 9B

Basic Java Constructs and Data Types Nuts and Bolts. Looking into Specific Differences and Enhancements in Java compared to C

Informatica e Sistemi in Tempo Reale

The Hexadecimal Number System and Memory Addressing

Glossary of Object Oriented Terms

Lecture 11 Doubly Linked Lists & Array of Linked Lists. Doubly Linked Lists

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

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

PCSpim Tutorial. Nathan Goulding-Hotta v0.1

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

The Java Series. Java Essentials I What is Java? Basic Language Constructs. Java Essentials I. What is Java?. Basic Language Constructs Slide 1

Symbol Tables. Introduction

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Molecular Dynamics Simulations with Applications in Soft Matter Handout 7 Memory Diagram of a Struct

Stack Allocation. Run-Time Data Structures. Static Structures

Introduction to Java

COMPUTER ORGANIZATION ARCHITECTURES FOR EMBEDDED COMPUTING

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

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Building Java Programs

C Programming Structure of a C18 Program

One Dimension Array: Declaring a fixed-array, if array-name is the name of an array

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

Reduced Instruction Set Computer (RISC)

Example of a Java program

An Incomplete C++ Primer. University of Wyoming MA 5310

Regression Verification: Status Report

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

CS 106 Introduction to Computer Science I

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Arrays in Java. Working with Arrays

Basics of I/O Streams and File I/O

Parallel and Distributed Computing Programming Assignment 1

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Passing 1D arrays to functions.

Chapter 5 Names, Bindings, Type Checking, and Scopes

The Little Man Computer

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

6.S096 Lecture 1 Introduction to C

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Linked Lists: Implementation Sequences in the C++ STL

Stacks. Linear data structures

An overview of FAT12

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

Lecture 11: Tail Recursion; Continuations

6. Control Structures

Using Files as Input/Output in Java 5.0 Applications

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

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

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Last Class: OS and Computer Architecture. Last Class: OS and Computer Architecture

Introduction to Python

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Transcription:

Let s translate this C function into assembly: CSE 378 Quiz Section #2 Coding in MIPS Assembly int arr[10] = {1,2,3,4,5,6,7,8,9,10; int compute_smth() { int i, sum=0; for (i = 0; i < 10; i++) { printf( result: %d,sum); return sum; So, now that we have our program, let us convert it to something that would be a bit easier to translate into assembly. Here is the reworked version: int arr[10] = {1,2,3,4,5,6,7,8,9,10; void main() { int i, sum; i = 0; sum = 0; To begin with, you should think about which variables/constants in the program are global and which are local. Global variables will need to be declared in a static data segment, for which you use the directive. This is typically written before your code, which goes into the segment. Which variables are global here, and which are local? Integer array arr is global. Static string result: is also global it s a global constant which has to be declared in the segment (there s no way to include strings in MIPS instructions!). So how do we actually declare globals? The directive specifies to the assembler that space for some data needs to be allocated in the static data portion of the program memory. Well, great, but how do we actually declare what data it is? Some keywords are helpful:.asciiz hello # reserves space for this string, and places # hello into it.space 50 # reserves space for 50 bytes, for general use.word 17,27 # reserves space for two words (each 4 bytes), # and initializes them to 17 and 27. Some others:.byte,.half,.double Before any declaration, you can include labels, which will serve as addresses to the data (example in a second). Note that ALL global variables, and NO non-global variables go into the static data section. A local variable, such as i or sum, does not belong here. So, for our global array, we could use the space keyword

to reserve enough memory. Since we also want to initialize it to 1-10 (e.g. for testing, etc.), we can also use.word and list the ten values separated by commas. Now, let s worry about the string result: in the first printf(). That can also be placed into the global data section, using the.asciiz keyword. Making the appropriate changes, our program now begins with // main function will follow What if we wanted to declare another word below msg? We need to be careful; what is wrong with saying: msg:.asciiz asdfg num:.word 42 num won t be aligned! ¾ chance you will crash (depending on length of preceding string). Fix? Use.align keyword..align N will align the next data item at a 2^N boundary. So here, you want to use: msg:.asciiz asdfg.align 2 # align at the next 4 byte (2^2) boundary num:.word 42 Next, let us specify (to SPIM) where the main() code is. We need to use the directive to begin the code segment. Everything that comes after it will be the actual executable code, and will get placed into memory as the text segment of the program. Note that ALL the code goes into this section. So, our function will now look like this:.globl main # declare main function as global, allow calls to it int i, sum; i = 0; sum = 0; Well, what about the local variables, like i and sum? Where would they go? Simple we can keep them in temporary-value registers ($t0 through $t7, or $8 through $15). One important rule to always adhere to: when programming in assembly, always always write down what each register represents. If you don t, careless errors WILL happen, and painful debugging will ensue. So, it is a good idea to create something like this somewhere in the file: # t2 = constant 10, for comparisons (we need to check when to break out of the while loop) # t3 = address of array elements (for loading from array)

Great, we re set! Now let s actually begin translating the code. Let s initialize the local variables to their necessary values:.globl main # declare main function as global, allow calls to it addi $t0, $0, 0 # clear i addi $t1, $0, 0 # clear sum la $t3, arr # load address of array into t4, pseudoinstr Notice how we initialized $t3 by using the arr label the (base) address of our array. Now, let us write the loop in assembly. Remember, that we will want to break out of it when $t0 is no longer less than $t2. Important: pay attention on how we walk through the array. We need to compute current element address for each iteration (for loading). Elements are separated by 4 bytes (easy to screw up)! There are a few ways to do this, this one is most efficient (another (see lecture) involves multiplication of i by 4 and adding to base here, we just add 4 to last element s address to get the current element address). Also, at the very end of the function, we will want to return back to the caller. Therefore, we will need to jump to where we were called from information stored in $ra. msg:.asciiz "Result: ".globl main # t2 = constant 10, for comparisons # t3 = address of array elements addi $t0, $0, 0 # clear i addi $t1, $0, 0 # clear sum la $t3, arr # load address of array into t4 loop: slt $t4, $t0, $t2 # compare, $t4 = i < sum? 1 : 0 beq $t4, $0, end # if i is not < 10, exit the loop

lw $t4, 0($t3) # load current array element into t4 add $t1, $t1, $t4 # add it to sum add $t0, $t0, 1 # increment i add $t3, $t3, 4 # increment current array element pointer j loop end: jr $ra What about those printfs? How do we get rid of those? We use the syscall instruction to perform these operations. To do so, we insert the system call number into register $v0, and its arguments into the $aregisters. And then we issue the syscall instruction. More specifically, to print an integer, we use systemcall number 1, and set $a0 to the integer we want to print. To print a string, we use system-call number 4, and set $a0 to contain the base address of the string. To read in an integer, we use system-call number 5. The result is returned in register $v0 (and the error code in $v1, so both v0 and v1 are destroy by any syscall!). These are far from the only system calls available to find out more about them, consult the full listings in the book. So, after applying these changes to our program, it becomes msg:.asciiz "Result: ".globl main # t2 = constant 10, for comparisons # t3 = address of array elements addi $t0, $0, 0 # clear i addi $t1, $0, 0 # clear sum la $t3, arr # load address of array into t4 loop: slt $t4, $t0, $t2 # compare, $t4 = i < sum? 1 : 0 beq $t4, $0, end # if i is not < 10, exit the loop lw $t4, 0($t3) # load current array element into t4 add $t1, $t1, $t4 # add it to sum add $t0, $t0, 1 # increment i add $t3, $t3, 4 # increment current array element pointer j loop end: addi $v0, $0, 4 la $a0, msg syscall # Now we print out result: string

addi $v0, $0, 1 # followed by the actual sum (which is in t1) add $a0, $t1, $0 syscall jr $ra And this is the full assembly version of our simple C program above.