Translating C code to MIPS



Similar documents
Typy danych. Data types: Literals:

Instruction Set Architecture

MIPS Assembly Code Layout

Reduced Instruction Set Computer (RISC)

Introduction to MIPS Assembly Programming

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

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

Review: MIPS Addressing Modes/Instruction Formats

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

More MIPS: Recursion. Computer Science 104 Lecture 9

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

Computer Systems Architecture

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

Summary of the MARIE Assembly Language

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

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

MIPS Assembler and Simulator

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

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

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

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

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

Assembly Language Programming

Figure 1: Graphical example of a mergesort 1.

EE282 Computer Architecture and Organization Midterm Exam February 13, (Total Time = 120 minutes, Total Points = 100)

Intel 8086 architecture

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

We will use the accumulator machine architecture to demonstrate pass1 and pass2.

Notes on Assembly Language

THUMB Instruction Set

6. Control Structures

5 MIPS Assembly Language

Chapter 7D The Java Virtual Machine

Keil C51 Cross Compiler

Instruction Set Architecture

Pseudo code Tutorial and Exercises Teacher s Version

EECS 427 RISC PROCESSOR

How To Write An Array In A Microsoft Zil

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 organization

The Operating System and the Kernel

Instruction Set Architecture (ISA) Design. Classification Categories

HC12 Assembly Language Programming

EC 362 Problem Set #2

Stack Allocation. Run-Time Data Structures. Static Structures

The Little Man Computer

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

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

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

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

Introduction to MIPS Programming with Mars

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

Moving from CS 61A Scheme to CS 61B Java

The stack and the stack pointer

Introduction to Java

Example of a Java program

Instruction Set Architecture (ISA)

Conditional Statements Summer 2010 Margaret Reid-Miller

X86-64 Architecture Guide

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

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

MICROPROCESSOR AND MICROCOMPUTER BASICS

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

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Instruction Set Design

Lecture 8: Binary Multiplication & Division

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

In this Chapter you ll learn:

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

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Object Oriented Software Design

Introduction. What is an Operating System?

Two-way selection. Branching and Looping

1 Abstract Data Types Information Hiding

Design of Digital Circuits (SS16)

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Tutorial on C Language Programming

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

Statements and Control Flow

VB.NET Programming Fundamentals

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Lab Work 2. MIPS assembly and introduction to PCSpim

An Introduction to the ARM 7 Architecture

Linux Process Scheduling Policy

Intermediate Code. Intermediate Code Generation

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

Exception and Interrupt Handling in ARM

1 Classical Universal Computer 3

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

CHAPTER 4 MARIE: An Introduction to a Simple Computer

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

CS352H: Computer Systems Architecture

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

PROBLEMS (Cap. 4 - Istruzioni macchina)

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

CPU Organization and Assembly Language

Volume I, Section 4 Table of Contents

umps software development

WAR: Write After Read

Transcription:

Translating C code to MIPS why do it C is relatively simple, close to the machine C can act as pseudocode for assembler program gives some insight into what compiler needs to do what's under the hood do you need to know how the carburetor works to drive your car? does your mechanic need to know?

Register conventions register conventions and mnemonics Number Name Use 0 $zero hardwired 0 value 1 $at used by assembler (pseudo-instructions) 2-3 $v0-1 subroutine return value 4-7 $a0-3 arguments: subroutine parameter value 8-15 $t0-7 temp: can be used by subroutine without saving 16-23 $s0-7 saved: must be saved and restored by subroutine 24-25 $t8-9 temp 26-27 $k0-1 kernel: interrupt/trap handler 28 $gp global pointer (static or extern variables) 29 $sp stack pointer 30 $fp frame pointer 31 $ra return address for subroutine Hi, Lo used in multiplication (provide 64 bits for result) hidden registers PC, the program counter, which stores the current address of the instruction being executed IR, which stores the instruction being executed

Arithmetic expression simple arithmetic expression, assignment int f, g, h, i, j; $s0 (g + h) - (i + j) f = (g + h) - (i + j); $s1 i + j $s2 h $s3 i $s4 j assume variables are assigned to $s0, $s1, $s2, $s3, $s4 respectively add $s0, $s1, $s2 # $s0 = g + h add $s1, $s3, $s4 # $s1 = i + j sub $s0, $s0, $s1 # f = (g + h) - (i + j)

Conditional: if simple if statement if ( i == j ) $s1 i i++ ; $s2 j j-- ; in C: if condition is true, we "fall through" to execute the statement if false, jump to next in assembly, we jump if condition is true need to negate the condition assuming $s1 stores i and $s2 stores j: bne $s1, $s2, L1 # branch if!( i == j ) addi $s1, $s1, 1 # i++ L1: addi $s2, $s2, -1 # j--

Conditional: if-else if-else if ( i == j ) $s1 i i++ ; $s2 j else j-- ; j += i ; As before, if the condition is false, we want to jump. bne $s1, $s2, ELSE # branch if!( i == j ) addi $s1, $s1, 1 # i++ ELSE: addi $s2, $s2, -1 # else j-- add $s2, $s2, $s1 # j += i What's wrong with this picture? Once we've done the if-body, we need to jump over the else-body bne $s1, $s2, ELSE # branch if!( i == j ) addi $s1, $s1, 1 # i++ j NEXT # jump over else ELSE: addi $s2, $s2, -1 # else j-- NEXT: add $s2, $s2, $s1 # j += i

Conditional: compound condition if-else with compound AND condition: short-circuiting if ( i == j && i == k ) // if ( <cond1> && <cond2> ) i++ ; // if body else $s1 i j-- ; // else body $s2 j j = i + k ; $s3 k Let <cond1> stand for (i == j) and <cond2> stand for (i == k). Short-circuiting occurs when <cond1> evaluates to false. The control flow then jumps over <cond2> and the if-body. If <cond1> evaluates to true, we also want to check <cond2>. If <cond2> evaluates false, we again jump, this time over the if-body, and to the else-body. If <cond2> is true, we fall-through to the if-body. bne $s1, $s2, ELSE # cond1: branch if!( i == j ) bne $s1, $s3, ELSE # cond2: branch if!( i == k ) addi $s1, $s1, 1 # if-body: i++ j NEXT # jump over else ELSE: addi $s2, $s2, -1 # else-body: j-- NEXT: add $s2, $s1, $s3 # j = i + k

Conditional: compound condition if-else with compound OR condition: short-circuiting use <cond1> to stand for (i == j) and <cond2> to stand for (i == k). if ( <cond1> <cond2> ) i++ ; // if-body $s1 i else $s2 j j-- ; // else-body $s3 k j = i + k ; Short-circuiting occurs when <cond1> evaluates to true If <cond1> is false, we also want to check <cond2> If <cond2> is false, we now jump to the else-body. If <cond2> is true, we fall through to the if-body. beq $s1, $s2, IF # cond1: branch if ( i == j ) # Notice branch on TRUE bne $s1, $s3, ELSE # cond2: branch if! ( i == k ) IF: addi $s1, $s1, 1 # if-body: i++ j NEXT # jump over else ELSE: addi $s2, $s2, -1 # else-body: j-- NEXT: add $s2, $s1, $s3 # j = i + k

Conditional: switch switch( i ) { case 1: i++ ; // falls through case 2: i += 2 ; $s1 i } break; $s4 temp case 3: i += 3 ; addi $s4, $zero, 1 # case 1: set temp to 1 bne $s1, $s4, C2_COND # false: branch to case 2 cond j C1_BODY # true: branch to case 1 body C2_COND: addi $s4, $zero, 2 # case 2: set temp to 2 bne $s1, $s4, C3_COND # false: branch to case 3 cond j C2_BODY # true: branch to case 2 body C3_COND: addi $s4, $zero, 3 # case 3: set temp to 3 bne $s1, $s4, EXIT # false: branch to exit j C3_BODY # true: branch to case 3 body C1_BODY: addi $s1, $s1, 1 # case 1 body: i++ C2_BODY: addi $s1, $s1, 2 # case 2 body: i += 2 j EXIT # break C3_BODY: addi $s1, $s1, 3 # case 3 body: i += 3 EXIT:

Loops: while $s1 i If statement uses branch instruction. $s2 j What about loops? $s3 k Example: while ( <cond> ) { L1: if ( <cond> ) { <while-body> <while-body> } goto L1 ; } If condition is true, execute body and go back, otherwise do next statement. while ( i < j ) { L1: if ( i < j ) { k++ ; k++ ; i = i * 2 ; i = i * 2 ; } goto L1 ; } L1: bge $s1, $s2, DONE # branch if! ( i < j ) addi $s3, $s3, 1 # k++ add $s1, $s1, $s1 # i = i * 2 j L1 # jump back to top of loop DONE:

Loops: for for ( <init> ; <cond> ; <update> ) { <for-body> } Equivalent while loop: <init>; <init>; while ( <cond> ) { L1: if ( <cond> ) { <for-body> <for-body> <update> <update> } goto L1 ; } DONE:

Array: C Problem: Given an array of int, calculate the sum of: all the elements in the array all the positive elements in the array all the negative elements in the array main () { int i, size = 10, sum, pos, neg; int arr[10] = {12, -1, 8, 0, 6, 85, -74, 23, 99, -30}; } sum = 0; pos = 0; neg = 0; for (i = 0; i < size; i++) { sum += arr[i]; if (arr[i] > 0) pos += arr[i]; if (arr[i] < 0) neg += arr[i]; } return 0;

Array: assembler.text.globl main main: la $s0, size lw $s1, 0($s0) ori $s2, $0, 0 ori $s3, $0, 0 ori $s4, $0, 0 # initialize registers # $s1 = size # $s2 = sum # $s3 = pos # $s4 = neg # <init> ori $s5, $0, 0 la $s6, arr # $s5 = i # $s6 = &arr # if (<cond>) L1: bge $s5, $s1, DONE # <for-body> lw $s7, 0($s6) # $s7 = arr[i] addu $s2, $s2, $s7 # sum += arr[i] blez $s7, NEG # if! (arr[i] > 0) addu $s3, $s3, $s7 # pos += arr[i];

j UPDATE # goto UPDATE NEG: bgez $s7, UPDATE # if! (arr[i] < 0) addu $s4, $s4, $s7 # neg += arr[i]; UPDATE: # <update> addi $s5, $s5, 1 # i++ addi $s6, $s6, 4 # move array pointer j L1 # goto L1 DONE: # initialize data.data size:.word 10 arr:.word 12, -1, 8, 0, 6, 85, -74, 23, 99, -30

This document was created with Win2PDF available at http://www.daneprairie.com. The unregistered version of Win2PDF is for evaluation or non-commercial use only.