IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.

Slides:



Advertisements
Similar presentations
Goal: Write Programs in Assembly
Advertisements

Review of the MIPS Instruction Set Architecture. RISC Instruction Set Basics All operations on data apply to data in registers and typically change the.
Dr. Ken Hoganson, © August 2014 Programming in R COURSE NOTES 2 Hoganson Language Translation.
Week 3. Assembly Language Programming  Difficult when starting assembly programming  Have to work at low level  Use processor instructions >Requires.
10/9: Lecture Topics Starting a Program Exercise 3.2 from H+P Review of Assembly Language RISC vs. CISC.
SPIM and MIPS programming
Fall EE 333 Lillevik 333f06-l4 University of Portland School of Engineering Computer Organization Lecture 4 Assembly language programming ALU and.
Instruction Set Architecture Classification According to the type of internal storage in a processor the basic types are Stack Accumulator General Purpose.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
Syscall in MIPS Xinhui Hu Yuan Wang.
MIPS Assembly Language Programming
Wannabe Lecturer Alexandre Joly inst.eecs.berkeley.edu/~cs61c-te
1 Computer Architecture MIPS Simulator and Assembly language.
Assembly Language Working with the CPU.
1 COMS 361 Computer Organization Title: Instructions Date: 9/28/2004 Lecture Number: 10.
 Procedures (subroutines) allow the programmer to structure programs making them : › easier to understand and debug and › allowing code to be reused.
1 Lecture-2 CSIT-120 Spring 2001 Revision of Lecture-1 Introducing Computer Architecture The FOUR Main Elements Fetch-Execute Cycle A Look Under the Hood.
Chapter 3 Assembly Language: Part 1. Machine language program (in hex notation) from Chapter 2.
CS 536 Spring Code generation I Lecture 20.
8 November Forms and JavaScript. Types of Inputs Radio Buttons (select one of a list) Checkbox (select as many as wanted) Text inputs (user types text)
1 Lecture-2 CS-120 Fall 2000 Revision of Lecture-1 Introducing Computer Architecture The FOUR Main Elements Fetch-Execute Cycle A Look Under the Hood.
Table 1. Software Hierarchy Levels.. Essential Tools An assembler is a program that converts source-code programs into a machine language (object file).
Implementation of a Stored Program Computer
Data Transfer & Decisions I (1) Fall 2005 Lecture 3: MIPS Assembly language Decisions I.
Basics Programming Concepts. Basics A computer program is a set of instructions to tell a computer what to do Machine language = circuit level language.
19/02/2009CA&O Lecture 05 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: PCSpim Tutorial Engr. Umbreen Sabir Computer Engineering.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
Comp Sci 251 Intro 1 Computer organization and assembly language Wing Huen.
Binary Arithmetic & Data representation
Lec 3: Data Representation Computer Organization & Assembly Language Programming.
Lecture 4: MIPS Instruction Set Reminders: –Homework #1 posted: due next Wed. –Midterm #1 scheduled Friday September 26 th, 2014 Location: TODD 430 –Midterm.
Chapter 19 Number Systems. Irvine, Kip R. Assembly Language for Intel-Based Computers, Translating Languages English: Display the sum of A times.
1 CS/COE0447 Computer Organization & Assembly Language Chapter 2 Part 4.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
Computer Architecture and Operating Systems CS 3230 :Assembly Section Lecture 3 Department of Computer Science and Software Engineering University of Wisconsin-Platteville.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Lecture 161 Lets examine a SPIM program in detail. io.asm This program is a simple routine which demonstrates input/output using assembly code. SPIM.
Computer Architecture CSE 3322 Lecture 3 Assignment: 2.4.1, 2.4.4, 2.6.1, , Due 2/3/09 Read 2.8.
Copyright 2006 by Timothy J. McGuire, Ph.D. 1 MIPS Assembly Language CS 333 Sam Houston State University Dr. Tim McGuire.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
8086/8088 Instruction Set, Machine Codes and Addressing Modes.
MIPS assembly syntax Home Assignment 3 Assigned. Deadline 2016 February 14, Sunday.
Translating Assembly Language to Machine Language.
CS 125 Lecture 3 Martin van Bommel. Overflow In 16-bit two’s complement, what happens if we add =
LECTURE 3 Translation. PROCESS MEMORY There are four general areas of memory in a process. The text area contains the instructions for the application.
Chapter 7: Low-Level Programming Languages Chapter 7 Low-Level Programming Languages Page 66 In order to execute instructions on a CPU, those instructions.
UFCFY5-30-1Multimedia Studio Coding for Interactive Media Fundamental Concepts.
CSCI-365 Computer Organization Lecture Note: Some slides and/or pictures in the following are adapted from: Computer Organization and Design, Patterson.
Computer Organization CS345 David Monismith Based upon notes by Dr. Bill Siever and notes from the Patternson and Hennessy Text.
Writing an Assembly-language program MIPS assembly language using MARS: MIPS Assembler and Runtime Simulator CS-2710 Dr. Mark L. Hornick 1.
Lec 3: Data Representation
Morgan Kaufmann Publishers
Lecture 4: MIPS Instruction Set
ACOE301: Computer Architecture II Labs
Chapter 3 Machine Language and Assembly Language.
Chapter 3 Machine Language and Assembly Language.
MIPS assembly syntax Comments
L7 – Assembler Directives
Installing and Using MARIE
Lecture 4: MIPS Instruction Set
Installing and Using MARIE
MARIE: An Introduction to a Simple Computer
Class 2.
Installing and Using MARIE
COMS 361 Computer Organization
Comp Org & Assembly Lang
MIPS Assembly Language Programming Computer Architecture
CS334: MIPS language _Mars simulator Lab 2_1
Introduction to Computer Science
Algoritmos y Programacion
Presentation transcript:

IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education

Assembly and Machine Language Machine language is a sequence of binary numbers that can be translated into instructions Assembly language is the text representation of machine language A compiler will take high level code, change it to assembly (low level code), which can then be converted directly into executable code

The MIPS Assembly Language MIPS Languages details: –One instruction per line. –Numbers are base-10 integers or Hex. –Variables: alphanumeric (can use numbers and integers for names) –Labels: identifiers starting at the beginning of a line followed by “:” –Comments: everything following # till end-of-line. –Instruction format: Space and “,” separated fields Example: add $s1,$s2,$s3 # comment (R-type) reg1, reg2, reg3 # comment

Directives in MIPS Directives tell the assembler what to do. They are not actual assembly, but help programmers write assembly code –Format “.” [arg1], [arg2]... –Examples. align 2 # align data on every word.data # start a data segment..text # start a code segment..ascii # store a string in memory..asciiz # store a null terminated string in memory.word w1, w2,..., wn # store n words in memory

Starting a program To begin your MIPS program you always have a few directives at the top: –.align 2 # tells assembler how to store data –.text # says the assembly comes next –main: # label for the start of your code –.data # put at the bottom, defines different data

Let’s look at a simple program Simple program to add two numbers together

Let's try another program int main() { int sum = 0; for (int k = 0; k <= 100; k++) { sum = sum + k*k; } cout << "the answer is " << sum << endl; }

And Now the Assembly Program.text # declare text segment.align 2 # align it on 4 -byte boundary main: li $3,0 # $3 = 0 (k = 0) li $4,0 # $4 = 0 (sum = 0) li $5,101 # R5 = 101; loop1: beq $3,$5,end # if (k == 101) go to end multu $3,$3 # k*k, result saved in HI and LO mflo $6 # move result from LO to $6 addu $4,$4,$6 # sum = sum + k*k addi $3,$3,1 # k++ j loop1 end: # Now we do output… li $v0,1 # prepare system call for print move $a0,$4 # move sum into $a0 to be printed syscall # print sum jr $31 # return to previous

Loops Loops do not have special commands in MIPS. You need to just use a branch and a jump statement together in order to achieve the same affect For example, in a for loop there are 3 parts –Initialization, Checking, and Post-Condition The initialization should be done before the loop starts The check needs to be done at the beginning of every loop The post-condition should be done at the end of every loop, but before you jump back to the start of the loop

Loop Example int k = 0; while (k < 10) { // do something k++; } li $t0,0 # k = 0 li $t1,10 # check 10 while: beq $t0,$t1,end # do something addi $t0,$t0,1 j while end: Post-Condition Initialization Condition

MIPS registers and naming MIPS registers are able to be addressed by their number ($1, $7) or by their name ($s1, $t2) The names allow for programmers to remember what the registers are traditionally used for.

System Calls All programming languages need a way to input and output data. MIPS is no different in that it can: –Read variables from input (like cin) –Output data to the screen (like cout) In order for MIPS to perform these operations it requires more steps that illustrate how the operating system handles input and output –Load a system call code into register $v0 (this tells the OS what I/O you want to do) Example: li $v0,1 –Load arguments into registers $a0,$a1 (this passes the data to the OS for outputting) –Do the syscall (system call), which makes the OS perform the operation –Results are returned in $v0 or $a0 (if the operation got data from the user, the data is in $v0 or $a0

MIPS system call codes

Sample program

Sample Program (continued)

Arrays in MIPS Arrays in MIPS are easy compared to C++. What you need to remember is that an array is a block of memory In order to make an array of different values, use MIPS code like this:.data x:.word 5,10,15,20 This creates a label "x" that points to the place in memory that holds the beginning of four words (32 bits) of data in a row.

Using Arrays in MIPS In order to use an array, you must increment the address of the register that stores the address of the array. main: la $t0,x # put address of x[0] into $t0 lw $t1,0($t0) # put $t1 = x[0] addi $t0,$t0,4 # change address to be x[1] lw $t2,0($t0) # put $t2 = x[1]

Functions and Recursion in MIPS Using functions or methods, especially with recursion, in MIPS requires extra work. A function call needs to use space (in the stack) in the memory in order to save information. This means it will use a place in memory to save important data like: –Values for arguments to the functions –Registers that don’t get modified in the function call –Local variables created within the function

A look at the stack during MIPS The stack pointer will change as each new function is called Each function can take space on the stack The space depends on how much data you want to save. The stack will grow down as you put more data in it 12 bytes $ra $s0 $a0 Function 1 12 bytes Function 2

What are the customs for function calls in MIPS? Step 1: Pass the arguments: –The first four arguments (arg0-arg3) are passed in registers $a0-$a3 –Remaining arguments are pushed onto the stack (in reverse order arg5 is at the top of the stack). Step 2: Save caller-saved registers –Save registers $s0-$s7 if they contain values you need to remember Step-3: Execute a jal instruction. –Jump and Link will save your return address in the $ra register, so that you can return to where you started Steps for the code that is calling a function

What are the customs for function calls in MIPS? Step 1: Establish stack frame. –Subtract the size of the data from the stack pointer. subiu $sp, $sp, –Typically, minimum size is 32 bytes (8 words). Step 2: Save callee saved registers in the frame. –Register $ra is saved if function uses another function. –Registers $s0-$s7 are saved if they are used. –Registers $a0-$a3 may need to be saved Steps for the function that is being called

What are the customs for function calls in MIPS? Step-1: Put returned values in registers $v0, [$v1]. –(if values are returned from the function) Step-2: Restore callee-saved registers. –Restore saved registers. [$ra, $s0 - $s7] that were placed in the stack to the registers where they belong Step-3: Pop the stack –Add the size of memory taken back to the $sp. addiu $sp, $sp, Step-4: Return –Jump to the address in $ra. jr $ra Steps for the code after the function is done

Sample Program: Factorial

Sample Program

Sample program with function Look at the file “hanoi.s” on the website Hanoi is a classic computer science program that uses recursion to solve a problem. We can see in the program how the stack is moved farther down each time, so that we save local variables and the return address.

Sample program using recursion The full text of the program is on the web. Here is the recursive part of Hanoi factorial: subu $sp, $sp, 8 # STACK PUSH sw $ra, 4($sp) # save return address sw $a0, 0($sp) # save register bgtz $a0, fact1 # branch if $a0 > 0 li $v0, 1 # return terminal value in # register $v0 lw $ra,4($sp) # put the return address back lw $a0,0($sp) # put $a0 back addu $sp, $sp, 8 # STACK POP jr $ra # return to starting point fact1: addi $a0, $a0, -1 # num = num -1 jal factorial # factorial ( num - 1 )

Intel 80x86 ISA MIPS is the instruction set for some computers and devices Pentium processors and other Intel processors use the 80x86 instruction set for computers that can run Windows It is more complicated than MIPS and a little harder to understand. It is only a 2 address ISA –(MIPS is 3 address, in Intel the operand is source and destination, like addu $t0,$t1) No load and store. An operand can be in memory, but does not need to be in a register

Summary We covered everything from opcodes to writing an assembly program. We went over how use registers, instructions and how to save things in memory so that we can have functions Also we went over system calls so we can do input and output