Adventures in Assembly Land

Similar documents
TSK3000A - Generic Instructions

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

The MIPS R2000 Instruction Set

MIPS Reference Guide

MIPS Instruction Format

SPIM Instruction Set

MIPS Assembly Language

Computer Architecture. The Language of the Machine

CSc 256 Midterm (green) Fall 2018

Question 0. Do not turn this page until you have received the signal to start. (Please fill out the identification section above) Good Luck!

MIPS Instruction Set

CSc 256 Midterm 2 Fall 2011

MIPS Instruction Reference

CSc 256 Midterm 2 Spring 2012

F. Appendix 6 MIPS Instruction Reference

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

Week 10: Assembly Programming

EEM 486: Computer Architecture. Lecture 2. MIPS Instruction Set Architecture

ECE 2035 Programming HW/SW Systems Fall problems, 7 pages Exam Two 23 October 2013

Programming the processor

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam One 4 February Your Name (please print clearly)

Q1: /30 Q2: /25 Q3: /45. Total: /100

ECE Exam I February 19 th, :00 pm 4:25pm

The MIPS Instruction Set Architecture

CSc 256 Final Fall 2016

Reduced Instruction Set Computer (RISC)

101 Assembly. ENGR 3410 Computer Architecture Mark L. Chang Fall 2009

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Two 11 March Your Name (please print) total

MIPS Assembly Language Programming

SPIM S20: A MIPS R2000 Simulator

Lec 10: Assembler. Announcements

Reduced Instruction Set Computer (RISC)

Introduction to MIPS Processor

Flow of Control -- Conditional branch instructions

Examples of branch instructions

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 22 September Your Name (please print clearly) Signed.

MIPS Assembly Language. Today s Lecture

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

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 21 October 2016

M2 Instruction Set Architecture

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 8 December 2014

Computer Architecture. MIPS Instruction Set Architecture

CPS311 - COMPUTER ORGANIZATION. A bit of history

CSE 2021: Computer Organization

ece4750-parc-isa.txt

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam One 19 September 2012

ECE 2035 Programming HW/SW Systems Fall problems, 6 pages Exam Two 23 October Your Name (please print clearly) Signed.

Project Part A: Single Cycle Processor

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 15B Computer Organization Spring 2010

Exam in Computer Engineering

CS 4200/5200 Computer Architecture I

ECE 2035 A Programming Hw/Sw Systems Spring problems, 8 pages Final Exam 29 April 2015

MIPS ISA. 1. Data and Address Size 8-, 16-, 32-, 64-bit 2. Which instructions does the processor support

CSc 256 Final Spring 2011

CSc 256 Midterm 1 Fall 2011

INSTRUCTION SET COMPARISONS

The MIPS R2000 Instruction Set

Assembly Programming

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

Mark Redekopp, All rights reserved. EE 357 Unit 11 MIPS ISA

CS 61c: Great Ideas in Computer Architecture

SPIM S20: A MIPS R2000 Simulator

instructions aligned is little-endian

Fundamentals of Computer Systems

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA MIPS ISA. In a CPU. (vonneumann) Processor Organization

ECE 2035 A Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 14 December 2016

A General-Purpose Computer The von Neumann Model. Concocting an Instruction Set. Meaning of an Instruction. Anatomy of an Instruction

Concocting an Instruction Set

ECE 2035 Programming Hw/Sw Systems Fall problems, 10 pages Final Exam 9 December 2013

EE108B Lecture 3. MIPS Assembly Language II

SPIM S20: A MIPS R2000 Simulator

ECE 2035 A Programming Hw/Sw Systems Fall problems, 8 pages Final Exam 9 December 2015

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Computer Organization & Design

ECE 2035 Programming HW/SW Systems Spring problems, 6 pages Exam Three 10 April 2013

Computer Systems and Architecture

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

Assemblers, Linkers, and the SPIM Simulator

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Mips Code Examples Peter Rounce

SPIM S20: A MIPS R2000 Simulator

ICS 233 COMPUTER ARCHITECTURE. MIPS Processor Design Multicycle Implementation

ENCM 369 Winter 2013: Reference Material for Midterm #2 page 1 of 5

Concocting an Instruction Set

MIPS%Assembly% E155%

USING A SIMULATOR. QUICK INTRODUCTION From various sources For cs470

software hardware Which is easier to change/design???

Lecture 2. Instructions: Language of the Computer (Chapter 2 of the textbook)

ECE232: Hardware Organization and Design. Computer Organization - Previously covered

Anne Bracy CS 3410 Computer Science Cornell University. [K. Bala, A. Bracy, E. Sirer, and H. Weatherspoon]

Problem 3: Theoretical Questions: Complete the midterm exam taken from a previous year attached at the end of the assignment.

SPIM S20: A MIPS R2000 Simulator

MACHINE LANGUAGE. To work with the machine, we need a translator.

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

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

The Program Counter. QtSPIM Register Display (Fixed-Point Registers) Erik Jonsson School of Engineering and Computer Science

RTL Model of a Two-Stage MIPS Processor

ece4750-tinyrv-isa.txt

Review: Organization. CS152 Computer Architecture and Engineering Lecture 2. Review of MIPS ISA and Design Concepts

Transcription:

Adventures in Assembly Land What is an Assembler ASM Directives ASM Syntax Intro to SPIM Simple examples L6 Simulator 1

A Simple Programming Task Add the numbers 0 to 4 10 = 0 + 1 + 2 + 3 + 4 In C : int i, sum; main() { sum = 0; for (i=0; i<5; i++) sum = sum + i; } Now let s code it in ASSEMBLY L6 Simulator 2

What IS an Assembler? A program for writing programs Machine Language: 1 s and 0 s toggled loaded into memory. (Did anybody ever really do that?) Assembly Language: Front panel of a classic PDP8e. The toggle switches were used to enter machine language..globl main main: subu $sp, $sp, 24 sw $ra, 16($sp) li $a0, 18 li $a1, 12 li $a2, 6 jal tak move $a0, $v0 UASM PGM 01101101 11000110 00101111 10110001... STREAM of bits to be loaded into memory Symbolic SOURCE text file ASSEMBLER Translator program Binary Machine Language Assembler: 1. A Symbolic LANGUAGE for representing strings of bits 2. A PROGRAM for translating Assembly Source to binary. L6 Simulator 3

Assembly Source Language An Assembly SOURCE FILE contains, in symbolic text, values of successive bytes to be loaded into memory... e.g..data 0x10000000.byte 1, 2, 3, 4.byte 5, 6, 7, 8.word 1, 2, 3, 4.asciiz "Comp 411".align 2.word 0xfeedbeef Specifies address where following values are loaded First four byte values Second four byte values Four WORD values (each is 4 bytes) A string of 9 ASCII bytes (8 + NULL) Align to next multiple of 4 (2 2 ) Hex encoded WORD Value Resulting memory dump: [0x10000000] 0x04030201 0x08070605 0x00000001 0x00000002 [0x10000010] 0x00000003 0x00000004 0x706d6f43 0x31313420 [0x10000020] 0x00000000 0xfeedbeef 0x00000000 0x00000000 Notice the byte ordering. This MIPS is little-endian (The least significant byte of a word or half-word has the lowest address) L6 Simulator 4

Assembler Syntax Assembler DIRECTIVES (Keywords prefixed with a. ) Control the placement and interpretation of bytes in memory.data <addr> Subsequent items are considered data.text <addr> Subsequent items are considered instructions.align N Skip to next address multiple of 2 N Allocate Storage.byte b 1, b 2,, b n Store a sequence of bytes (8-bits).half h 1, h 2,, h n Store a sequence of half-words (16-bits).word w 1, w 2,, w n Store a sequence of words (32-bits).ascii string Stores a sequence of ASCII encoded bytes.asciiz string Stores a zero-terminated string.space n Allocates n successive bytes Define scope.globl sym Declares symbol to be visible to other files.extern sym size Sets size of symbol defined in another file (Also makes it DIRECTly addressable) L6 Simulator 5

More Assembler Syntax Assembler COMMENTS All text following a # (sharp) to the end of the line is ignored Assembler LABELS Labels are symbols that represent memory addresses. Labels take on the values of the address where they are declared. Labels declarations appear at the beginning of a line, and are terminated by a colon. Labels can be established for data items as well as instructions e. g..data item:.word 1 # a data word.text start:.word 0x00821820 # add $3, $4, $2.word 0x00031a00 # sll $3, $3, 8.word 0x306300ff # andi $3, $3, 0xff While having an assembler helps, coding like this is still painful. (Don t actually do this!) L6 Simulator 6

Our Example: Variable Allocation Two integer variables (by default 32 bits in MIPS).data.globl sum.globl i sum:.space 4 i:.space 4.data assembler directive places the following words into the data segment.globl directives make the sum and I variables visible to all other assembly modules.space directives allocate 4 bytes fr each variable L6 Simulator 7

Even More Assembler Syntax Assembler PREDEFINED SYMBOLS Register names and aliases $0-$31, $zero, $v0-$v1, $a0-$a3, $t0-$t9, $s0-$s7, $at, $k0-$k1, $gp, $sp, $fp, $ra Assembler MNEMONICS Symbolic representations of individual instructions add, addu, addi, addiu, sub, subu, and, andi, or, ori, xor, xori, nor, lui, sll, sllv, sra, srav, srl, srlv, div, divu, mult, multu, mfhi, mflo, mthi, mtlo, slt, sltu, slti, sltiu, beq, bgez, bgezal, bgtz, blez, bltzal, bltz, bne, j, jal, jalr, jr, lb, lbu, lh, lhu, lw, lwl, lwr, sb, sh, sw, swl, swr, rfe pseudo-instructions (some mnemonics are not real instructions) abs, mul, mulo, mulou, neg, negu, not, rem, remu, rol, ror, li, seq, sge, sgeu, sgt, sgtu, sle, sleu, sne, b, beqz, bge, bgeu, bgt, bgtu, ble, bleu, blt, bltu, bnez, la, ld, ulh, ulhu, ulw, sd, ush, usw, move,syscall, break, nop L6 Simulator 8

Actual Code Next we write ASSEMBLY code using the instruction mnemonics A common convention, which originated with the C programming language, is for the entry point (starting location) of a program to named main..text 0x80000080.globl main main: add $8,$0,$0 # sum = 0 add $9,$0,$0 # for (i = 0;... loop: addu $8,$8,$9 # sum = sum + i; addi $9,$9,1 # for (...;...; i++ slti $10,$9,5 # for (...; i<5; bne $10,$0,loop end: beq $0,$0,end Bookkeeping: 1) Register $8 is allocated as the sum variable 2) Register $9 is allocated as the i variable L6 Simulator 9

SPIM Simulation Let s tryout our Example We ll use the SPIM (MIPS backwards) integrated ASSEMBLER, SIMULATOR, and DEBUGGER. Links to SPIM program and docs are available at Links section of the course website (You ll need to download it for your next Problem set) L6 Simulator 10

Getting Started SPIMing The following hints will get you started with SPIM By default a small fragment of code is loaded called the kernel. We will discuss the role of this code in detail, later in the course. Basically it s primary job is to branch to the main label of your code. It does so in approximately 4 instructions. We will use a raw version of the machine, w/o a kernel You can single-step the machine by pressing [F10] The results of the execution of each instruction are reflected in the register and memory location values Illegal operations result in an exception which causes your code to automatically jump back the kernel. For our purposes now, this will be due to a bug in your program. Refer to the manual for more fancy usage, such as setting and executing to breakpoints A quick demo L6 Simulator 11

RAW SPIM You ll need to change the default settings as follows: Note: For now, the only option that we will enable on the Simulator Setting menu is to allow pseudo instructions L6 Simulator 12

A Slightly More Challenging Program Add 5 numbers from a list sum = n 0 + n 1 + n 2 + n 3 + n 4 In C : int i, sum; int a[5] = {7,8,9,10,8}; main() { sum = 0; for (i=0; i<5; i++) sum = sum + a[i]; } Once more let s encode it in assembly L6 Simulator 13

Variable Allocation We cheated in our last example. Generally, variables will be allocated to memory locations, rather than registers (Though clever optimization can often avoid it). This time we add the contents of an array Note the use of the.extern assembler directive here. SPIM *requires*, that directly accessed variables are declared as externs..data.extern sum 4.extern i 4.extern a 20 sum:.space 4 i:.space 4 a:.word 7,8,9,10,8 Arrays have to be in memory. Why?.word allows us to initialize a list of sequential words in memory. The label represents the address of the first word in the list, or the name of the array L6 Simulator 14

The New Code Note the small changes:.text 0x80000080.globl main main: sw $0,sum # sum = 0; sw $0,i # for (i = 0; lw $9,i # allocate register for i lw $8,sum # allocate register for sum loop: sll $10,$9,2 # covert "i" to word offset lw $10,a($10) # load a[i] addu $8,$8,$10 # sum = sum + a[i]; sw $8,sum # update variable in memory addi $9,$9,1 # for (...;...; i++ Let s try sw $9,i # update memory it in SPIM slti $10,$9,5 # for (...; i<5; bne $10,$0,loop end: beq $0,$0,end L6 Simulator 15

A Little Weirdness The Assembler rewrote one of our instructions. What s going on? L6 Simulator 16

A Coding Challenge What is the largest Fibonacci number less than 100? Fibonacci numbers: F i+1 = F i + F i-1 F 0 = 0 F 1 = 1 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, In C int x, y; main() { x = 0; y = 1; while (y < 100) { int t = x; x = y; y = t + y; } } L6 Simulator 17

MIPS Assembly Code In assembly.data.extern x 4.extern y 4 x:.space 4 y:.space 4.text 0x80000080.globl main main: sw $0,x # x = 0; addi $9,$0,1 # y = 1; sw $9,y lw $8,x while: # while (y < 100) { slti $10,$9,100 beq $10,$0,endw add $10,$0,$8 # int t = x; add $8,$0,$9 # x = y; sw $8,x add $9,$10,$9 # y = t + y; sw $9,y beq $0,$0,while # } endw: beq $0,$0, endw # answer is x L6 Simulator 18

Next Time Parameterized Programs Procedures Stacks MIPS procedure linkage conventions L6 Simulator 19