Basic Computer Operations and Assembly Language Programming

Similar documents
CSc 256 Midterm 1 Fall 2011

CSc 256 Midterm (green) Fall 2018

CSc 256 Midterm 2 Fall 2011

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

CSc 256 Midterm 2 Spring 2012

CSc 256 Final Fall 2016

CENG3420 Lecture 03 Review

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

MIPS Assembly Language

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

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

Introduction to MIPS Processor

CISC 662 Graduate Computer Architecture. Lecture 4 - ISA

Chapter 2A Instructions: Language of the Computer

MIPS Assembly Language. Today s Lecture

Instruction Set Architecture part 1 (Introduction) Mehran Rezaei

Instructions: Language of the Computer

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

Computer Organization MIPS ISA

Topic Notes: MIPS Instruction Set Architecture

SPIM Instruction Set

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

ELEC / Computer Architecture and Design Fall 2013 Instruction Set Architecture (Chapter 2)

MIPS Instruction Format

Machine Language Instructions Introduction. Instructions Words of a language understood by machine. Instruction set Vocabulary of the machine

INSTRUCTION SET COMPARISONS

Lecture 6 Decision + Shift + I/O

Computer Architecture

Reduced Instruction Set Computer (RISC)

MIPS R-format Instructions. Representing Instructions. Hexadecimal. R-format Example. MIPS I-format Example. MIPS I-format Instructions

Introduction to the MIPS. Lecture for CPSC 5155 Edward Bosworth, Ph.D. Computer Science Department Columbus State University

Assembly Language Programming. CPSC 252 Computer Organization Ellen Walker, Hiram College

Computer Science 324 Computer Architecture Mount Holyoke College Fall Topic Notes: MIPS Instruction Set Architecture

Today s topics. MIPS operations and operands. MIPS arithmetic. CS/COE1541: Introduction to Computer Architecture. A Review of MIPS ISA.

I ve been getting this a lot lately So, what are you teaching this term? Computer Organization. Do you mean, like keeping your computer in place?

The MIPS Instruction Set Architecture

Computer Architecture. The Language of the Machine

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

MIPS Assembly Language Guide

Lab 3 (All Sections) Prelab: MIPS-Control Instructions

Chapter 2. Instructions: Language of the Computer. Adapted by Paulo Lopes

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

EEC 581 Computer Architecture Lecture 1 Review MIPS

Programmable Machines

COMPSCI 313 S Computer Organization. 7 MIPS Instruction Set

Reduced Instruction Set Computer (RISC)

Computer Architecture Instruction Set Architecture part 2. Mehran Rezaei

Programmable Machines

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

Chapter 2. Instructions: Language of the Computer. HW#1: 1.3 all, 1.4 all, 1.6.1, , , , , and Due date: one week.

2.7 Supporting Procedures in hardware. Why procedures or functions? Procedure calls

MIPS Assembly Language Programming

Lab 3 (Sections 300, 301 and 302) Prelab: MIPS-Control Instructions

Computer Architecture. MIPS Instruction Set Architecture

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

MIPS Assembly Language Programming

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

MIPS ISA and MIPS Assembly. CS301 Prof. Szajda

Computer Organization and Structure. Bing-Yu Chen National Taiwan University

MIPS Instruction Set

Anne Bracy CS 3410 Computer Science Cornell University. See P&H Chapter: , , Appendix B

Processor design - MIPS

Chapter 2. Computer Abstractions and Technology. Lesson 4: MIPS (cont )

Stored Program Concept. Instructions: Characteristics of Instruction Set. Architecture Specification. Example of multiple operands

Announcements HW1 is due on this Friday (Sept 12th) Appendix A is very helpful to HW1. Check out system calls

MIPS Memory Access Instructions

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

1 5. Addressing Modes COMP2611 Fall 2015 Instruction: Language of the Computer

Lecture Topics. Announcements. Today: Integer Arithmetic (P&H ) Next: continued. Consulting hours. Introduction to Sim. Milestone #1 (due 1/26)

Compiling Techniques

Levels of Programming. Registers

A Processor. Kevin Walsh CS 3410, Spring 2010 Computer Science Cornell University. See: P&H Chapter , 4.1-3

CS3350B Computer Architecture MIPS Instruction Representation

CS31001 COMPUTER ORGANIZATION AND ARCHITECTURE. Debdeep Mukhopadhyay, CSE, IIT Kharagpur. Instructions and Addressing

Course Administration

Course Administration

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

Lecture 7: Examples, MARS, Arithmetic

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

Instructions: MIPS arithmetic. MIPS arithmetic. Chapter 3 : MIPS Downloaded from:

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

1/26/2014. Previously. CSE 2021: Computer Organization. The Load/Store Family (1) Memory Organization. The Load/Store Family (2)

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

Concocting an Instruction Set

Processor. Han Wang CS3410, Spring 2012 Computer Science Cornell University. See P&H Chapter , 4.1 4

CENG3420 L03: Instruction Set Architecture

Chapter 2. Instructions:

Instructions: MIPS ISA. Chapter 2 Instructions: Language of the Computer 1

ECE232: Hardware Organization and Design

CS 61c: Great Ideas in Computer Architecture

EE108B Lecture 3. MIPS Assembly Language II

TSK3000A - Generic Instructions

4.2: MIPS function calls

Rui Wang, Assistant professor Dept. of Information and Communication Tongji University.

ENGN1640: Design of Computing Systems Topic 03: Instruction Set Architecture Design

17. Instruction Sets: Characteristics and Functions

ISA: The Hardware Software Interface

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

Midterm. Sticker winners: if you got >= 50 / 67

Lecture 4: MIPS Instruction Set

Transcription:

Chapter 2: Basic Computer Operations and Assembly Language Programming Topics: Programmer s view of computer system Instruction Execution Cycle Simple Program Translation with MAL page 2.1

Programmer s View of Computer System page 2.2

System bus: Input/output (IO) devices: Central Processing Unit (CPU): Some CPUs: Sun Sparc, PowerPC, Intel x86 For CSc 310, use MIPS CPU (Silicon Graphics) Main memory: page 2.3

Units of storage: bit nibble byte halfword word doubleword MIPS CPU: * each memory address is 32 bits Memory is byte-addressed: * each increment of the address refers to the next byte page 2.4

Memory viewed as array of bytes: address contents 0x0000 0000 0x0000 0001 0x0000 0002 0x0000 0003 0x0000 0004 0x0000 0005 0x0000 0006 0x0000 0007 0x0000 0008 0x0000 0009 0x0000 000a 0x0000 000b page 2.5

Memory viewed as array of 32-bit words: address contents 0x0000 0000 0x0000 0004 0x0000 0008 0x0000 000c 0x0000 0010 0xffff fffc Memory byte-order: MIPS memory is big-endian: the byte with the lowest address is at the most significant end (left-most, end with highest value) [most commercial CPUs are big-endian, except Intel x86; little-endian:] page 2.6

Contents of a byte at address 0x0000 0004: Contents of a byte at address 0x0000 0006: Contents of a word at address 0x0000 0004 (contents of 4 bytes at 0x0000 0004 to 0x0000 0007): Aligned versus misaligned: If a word in memory is aligned, the 4 bytes within the word do not cross word boundaries (words at 0, 4, 8, 0xc, 0x10, 0x14...) If a word is misaligned, its 4 bytes cross word boundaries. (words at 1, 2, 3, 5, 6, 7...) How to tell if a word address is aligned? 1) look at least significant (rightmost) digit 2) look at least significant 2 bits page 2.7

Size of memory address (32 bits in MIPS, Pentiums) determines the amount of memory that can be addressed by programs running on a CPU. 32-bit address means a program can see 1 KB = 1 MB = 1 GB = However, this does not mean that your PC has the same amount of physical memory! page 2.8

Parts of CPU Control unit: Arithmetic Logic Unit (ALU): Registers: Program counter: page 2.9

MIPS has 32 registers, each a 32-bit word Two registers with restricted use: $0 $1 $0 $1 32 $30 $31 page 2.10

Registers have alternate names: $2 - $3 also called $v0 - $v1 $4 - $7 also called $a0 - $a3 $8 - $15 also called $t0 - $t7 $16 - $23 also called $s0 - $s7 $24 - $25 also called $t8 - $t9 $26 - $27 also called $k0 - $k1 $28 also called $gp $29 also called $sp $30 also called $s8 $31 also called $ra For now, use $s0 - $s8 (always safe), or $t0 - $t9. More on these later, when we talk about function calls. page 2.11

High Level Language Program: statement 1 statement 2 statement 3 Assembly Language Program: instruction 1 instruction 2 instruction 3 Von Neumann (or Eckert/Mauchly) architecture: 1) programs and data are stored in the same memory 2) instructions are executed in sequential order, except for branches/jumps page 2.12

What happens when a user runs a program? 1) user types a.out\n 2) CPU finds a.out file 3) CPU loads memory with program code and data from file page 2.13

Typical assembly language format: operation operand1 operand2 operand3... Operation: what the instruction does (+, -) Operands: variables Variables are allocated to locations in memory, or to registers For example ($s0-$s3 contain int variables): add $s0,$s1,$s2 means $s0 = $s1 + $s2 sub $s3,$s1,$s2 means $s3 = $s1 - $s2 For our simple example, assume: each instruction is 32 bits the program starts at PC = 0x400020 (These are typical addresses for MIPS/SPIM) page 2.14

CPU goes through 5 main steps to process each instruction. Instruction Fetch (IF): Instruction Decode (ID): Execute (EX): Memory Access (MEM): Write Result (WR): page 2.15

page 2.16 PC

page 2.17

Levels of Assembly Language in MIPS MAL (MIPS assembly language) TAL (True assembly language) page 2.18

MIPS (like PowerPC, Sun Sparc, Intel IA64) is a load/store architecture. This means: Given a program, where to allocate its variables? Option 1: in memory Advantage: more general Disadvantages: slower access, more code Option 2: in registers Advantages: fast access, less code Disadvantages: a) may not have enough registers for all vars b) must keep track of where each var is A good compiler will try to allocate frequently used variables to registers. page 2.19

First MAL Program (variables in memory) int a=0,b=1,c=2,d=3,e=4; int main(void) { b = e; a = b + c; d = b - c; e = e - 1; } To move an int x from memory to register $s0: [load word] To move an int x from register $s0 to memory: [store word] More steps for each statement: 1) move variables from memory into registers 2) perform computation 3) move result from register into memory [use registers as temporaries] page 2.20

load word (from memory to register): lw R, label [R is any register] contents of R = contents of aligned word at label (in memory) Example: lw $13, x 13 x: 0x10010000 0x1234 abcd 0x10010004 page 2.21

store word (from register to memory): sw R, label [R is any register] contents of aligned word at label (in memory) = contents of R Example: sw $13, x x: 0x10010000 0x10010004 13 0x89abcdef page 2.22

.data a:.word 0 main:.text page 2.23

[see Example 2.1] page 2.24

Labels: * mark locations in memory * each label associated with a memory address Data allocation section: * like variable declaration section * global variables only * allocates some size of memory * no (actual) type (!) Assembler directives: (directions for assembler; not program code).data label:.word?? label:.word??:n label:.byte??:n label:.float??:n page 2.25

.text label:.space N label:.asciiz abc2 label:.ascii xyz page 2.26

Same MAL Program (variables in registers) int a=0,b=1,c=2,d=3,e=4; int main(void) { b = e; a = b + c; d = b - c; e = e - 1; } Compiler decides: a $s0 b $s1 c $s2 d $s3 e $s4 [see Example 2.2] page 2.27

.data [left blank for this example...].text main: page 2.28

MAL arithmetic/logic instructions int x,y,z; Compiler decides: x $s0 y $s1 z $s2 MAL: li $s0,4 [or any constant] contents of $s0 = 4 C/C++: x = 4 MAL: move $s0,$s1 contents of $s0 = contents of $s1 C/C++: x = y MAL: add $s0,$s1,$s2 contents of $s0 = contents of $s1 + contents of $s2 C/C++: x = y+z page 2.29

MAL: sub $s0,$s1,$s2 contents of $s0 = contents of $s1 - contents of $s2 C/C++: x = y-z MAL: mul $s0,$s1,$s2 contents of $s0 = contents of $s1* contents of $s2 C/C++: x = y*z MAL: div $s0,$s1,$s2 [integer division] contents of $s0 = contents of $s1/ contents of $s2 C/C++: x = y/z MAL: rem $s0,$s1,$s2 contents of $s0 = contents of $s1% contents of $s2 C/C++: x = y % z page 2.30

Compound arithmetic statements Example: D = b*b - 4*a*c; Use temporary variables, break into simple statements. Compiler decides: a $s0 b $s1 c $s2 D $s3 temp $t0 temp1 $t1 page 2.31

MAL Input/output To perform input/output (I/O), the instruction is used to call system routines. Before is executed, 1) $v0 contains a code number to indicate the type of operation (read an int, print an int etc) 2) $a0 contains additional arguments (e.g. integer to be printed) 3) result from user input appear in $v0 C++: int n=9; cout << n; cin >> n; page 2.32

C++: char str[] = "abc"; cout << str; MAL: str:.asciiz "abc" page 2.33

Selection (if, if-else) C++: if (x > y) sum = sum + x; Suppose x is $s0, y is $s1, sum is $s2. MAL conditional branches: if (condition true) goto label; else execute next instruction beq op1, op2, label [branch if equal] if (contents of op1 == contents of op2) goto label else execute next instruction op1 is any register op2 is any register or constant Example: beq $s0, $s1, label (test $s0 == $s1) beq $s0,4,label (test $s0 == 4) page 2.34

Other conditional branches: bne op1,op2, label [test op1!= op2] bgt op1,op2, label [test op1 > op2] bge op1,op2, label [test op1 >= op2] blt op1,op2, label [test op1 < op2] ble op1,op2, label [test op1 <= op2] Conditional branches that test zero: beqz op1, label [test op1!= 0] bnez op1, label [test op1!= 0] bgtz op1, label [test op1 > 0] bgez op1, label [test op1 >= 0] bltz op1, label [test op1 < 0] blez op1, label [test op1 <= 0] page 2.35

Original: if (x > y) sum = sum + x; Rewrite: if (x<= y) goto skip; sum = sum + x; skip: [next statement] Compiler decides: x is $s0, y is $s1, sum is $s2 MAL: ble $s0,$s1, skip add $s2, $s2, $s0 skip: [next instruction] [see Example 2.3] page 2.36

Same example, variables in memory: if (x > y) sum = sum + x; MAL:.data x:.word 0 y:.word 0 sum:.word 0.text main: [some code not shown...] page 2.37

[see Example 2.4] page 2.38

Making structured flowcharts: 1) each box is either an arithmetic statement or a test 2) arithmetic statements have only one exit 3) tests have two exits, yes and no 4) boxes should be arranged in a straight line 5) no conditions are in main line of flow 6) yes conditions are side branches To translate from C++ code to flowchart: 1) write down each statement or test, in order. 2) change test conditions if necessary Flow chart for if (x>y) sum = sum+x; : page 2.39

Unconditional branches (goto): b label [branch; goto label] j label [jump; goto label] If-else statements: Example (suppose num is $s0, flag is $s1) if (num > 0) flag = 1; else if (num < 0) flag = -1; else flag = 0; page 2.40

Flow charts: MAL version (num is $s0, flag is $s1): [see Example 2.5] page 2.41

If statements with and conditions: if ((x > y) && (x < 0)) flag = 23; (Suppose x is $s0, y is $s1, flag is $s2) Flow chart: MAL code: page 2.42

If statements with or conditions: if ((a!= 0) (b < 5)) grade = -1; (Suppose a is $s0, b is $s1, grade is $s2) Flow chart: MAL code: page 2.43

Repetition: loops C++: sum = 0; for (i=1; i<= limit; i++) sum = sum + i; Suppose sum is $s0, i is $s1, limit is $s2. Flow chart and MAL code (version 1): page 2.44

Flow chart and MAL code (version 2): Version 2 is more efficient. All your loops should follow version 2, with test condition at the bottom of the loop. [see Example 2.6] page 2.45

do-while loop: while loop: page 2.46

Truth table for simple logical operations y z y and z y or z y nand z y nor z y xor z 0 0 0 1 1 0 1 1 Bitwise logical operations: logical operation applied to each bit of operand (Suppose x is $s0, y is $s1, z is $s2) C/C++: x = ~y (different from x =!y) MAL: not $s0,$s1 contents of $s0 = not (contents of $s1) Example: int x, y = 0x12345678; x = ~y; x becomes: page 2.47

C/C++: x = y& z MAL: and $s0,$s1,$s2 contents of $s0 = contents of $s1 and contents of $s2 C/C++: x = y z MAL: or $s0,$s1,$s2 contents of $s0 = contents of $s1 or contents of $s2 C/C++: no nor operator MAL: nor $s0,$s1,$s2 contents of $s0 = contents of $s1 nor contents of $s2 C/C++: x = y ^ z MAL: xor $s0,$s1,$s2 contents of $s0 = contents of $s1 xor contents of $s2 page 2.48

Shifts and rotates C/C++: x = y << AMT (x in $s0, y in $s1) MAL shift left logical: sll $s0, $s1, AMT contents of x = contents of y shifted left by AMT bits, empty bits filled with zeros Example: y = 0x89abcdef // in $s1 After sll $s0,$s1,3 $s0 becomes: C/C++: x = y >> AMT (x,y declared unsigned) MAL shift right logical: srl $s0,$s1, AMT contents of x= contents of y shifted right by AMT bits, empty bits filled with zeros Example: y = 0x89abcdef // in $s1 page 2.49

After srl $s0,$s1,3 $s0 becomes: C/C++: x = y >> AMT (x,y declared int) MAL shift right arithmetic: sra $s0,$s1, AMT contents of x = contents of y shifted right by AMT bits, empty bits filled with sign bit Example: y = 0x89abcdef // in $s1 After sra $s0,$s1,3, $s0 becomes: page 2.50

Relationship between shifts and arithmetic operations: shift left n bits equivalent to shift right n bits equivalent to must watch for overflow! page 2.51

C/C++: no rotate operator MAL rotate left:rol $s0,$s1, AMT contents of $s0 = contents of $s1 rotated left by AMT bits Example: $s1 = 0x89abcdef After rol $s0,$s1,3 $s0 becomes: C/C++: no rotate operator MAL rotate right:ror $s0,$s1, AMT contents of $s0 = contents of $s1 rotated right by AMT bits page 2.52

Summary of MAL arithmetic/logic instructions [op1, op2, op3 are operands, usually registers or constants] li op1, CONSTANT move op1, op2 add op1, op2, op3 sub op1, op2, op3 mul op1, op2, op3 div op1, op2, op3 rem op1, op2, op3 not op1, op2 and op1, op2, op3 or op1, op2, op3 nand op1, op2, op3 nor op1, op2, op3 xor op1, op2, op3 sll op1, op2, AMT srl op1, op2, AMT sra op1, op2, AMT rol op1, op2, AMT ror op1, op2, AMT page 2.53

Summary of MAL branch instructions [op1, op2, op3 are operands, usually registers or constants] b label j label beq op1, op2, label bne op1, op2, label bgt op1, op2, label bge op1, op2, label blt op1, op2, label ble op1, op2, label beqz op1, label bnez op1, label bgtz op1, label bgez op1, label bltz op1, label blez op1, label Instruction for requesting system services: page 2.54

Some useful Unix commands lslist files in current directory cat <filename>display contents of file called <filename> on screen more <filename>like "cat", but page by page cd <dir-name>change to directory with name <dir-name> cp <oldfile> <newfile>make a copy of <oldfile>; the new copy is called <newfile> mv <oldfile> <newfile>change the name of <oldfile> to <newfile> Some useful spim commands exit -- Exit from the simulator read "FILE" -- Read FILE of assembly code into memory load "FILE" -- Same as read run <ADDR> -- Start the program at optional ADDRESS step -- execute the next TAL instruction in the program step <N> -- Step the program for N instructions continue -- Continue program execution without stepping print $N -- Print register N print $fn -- Print floating point register N print ADDR -- Print contents of memory at ADDRESS print PC -- Print address of current instruction (not yet executed) reinitialize -- Clear the memory and registers breakpoint <ADDR> -- Set a breakpoint at address delete <ADDR> -- Delete all breakpoints at address list -- List all breakpoints. -- Rest of line is assembly instruction to put in memory <cr> -- Newline reexecutes previous command? -- Print this message Most commands can be abbreviated to their unique prefix e.g., ex, re, l, ru, s, p page 2.55

Programming Style Guidelines Your projects will be graded on both correctness and clarity. Since you are not the only person who has to read your programs, you must comment them adequately so other people can understand them easily. 1) Program header All your projects must start with a program header, something like this (fill in the information): # # CSc 310 Project?? # Name:???? # Date:???? # Description: This program does???? # The Description should be a brief statement of what the program does, what the user has to enter (if anything), and what results the program prints out. 2) Indentation Your MIPS assembly language program should be neatly divided into four columns which line up properly. (Hint: using "tabs" is a good way to line things up.) The first column should contain labels only. The second column should contain instruction operations only. The third column should contain the list of operands for that instruction. The fourth column should contain comments (if any). Make sure that each line of your program code is less than 80 characters. Tabs should be set to 8 characters. 3) Commenting style You should write your comments so that even someone who is not familiar with MIPS assembly language can follow what is happening in the program. Describe all important variables used in the.data section. Identify which registers are used as which variables, where appropriate. Describe what the instructions are doing within the context of the program, *not* the definition for each instruction. You don't have to comment every single instruction, but it should be clear page 2.56

what those lines of your program are doing. For example, this is good commenting style: # i $s0 # x $s1 # sum $s2 main: li $s0,1 # for (i=1; i<=10;i++){ loop: li $v0,5 # cin >> x; move $s1,$v0 blez $s1,skip # if (x > 0) add $s2,$s2,$s0 # sum=sum+i; skip: add $s0,$s0,1 # } ble $s0,10,loop Notice that C/C++ style indentations within the comments helps to make the comments more readable. Just by reading the comments, we can tell that the "sum=sum+i" line is executed if x > 0, and all the statements from "cin..." through "sum =..." belong inside the for loop. In contrast, thiis is much less readable: loop: li $v0,5 # cin >> x; move $s1,$v0 blez $s1,skip # if (x <= 0) goto skip add $s2,$s2,$s0 # sum=sum+i; skip: add $s0,$s0,1 # i++; ble $s0,10,loop # if (i <= 10) goto loop Almost every single instruction is commented, but it's harder to read the comments. It's not obvious that we have a loop, and what goes inside the loop body. However, this doesn't always mean that fewer comments are better! As much as possible, your comments should read like a high-level language program, with extra text to explain what is happening, if necessary. (Sometimes, it's not easy to make your comments look like a high-level language program.) You should always try to describe the algorithm, the "big picture" of what is happening with the code, and not just describe every single instruction and get lost in the low level details. The programming examples that you'll use in this class, in ~whsu/310/progs, will give you many examples of what the instructor considers good commenting style. (However, the programs provided in CSc 311 are often not properly commented; the difference should be obvious.) page 2.57

Complete Program Listings for Chapter 2 ( also found in ~whsu/310/progs on libra) 1) First MAL program (variables in memory) 2) First MAL program (variables in registers) 3) If statement example (variables in memory) 4) If statement example (variables in registers) 5) If-else example (variables in registers) 6) For loop example (variables in registers) page 2.58

# # CSc 310 Example 2.1: First MAL program, variables in memory # Name: William Hsu # Date: 7/11/2000 # Description: Demonstrates simple arithmetic instructions # with variables in memory # # int a=0,b0=1,c=2,d=3,e=4; # # int main(void) # { # b0 = e; # a = b0 + c; # d = b0 - c; # e = e - 1; # # cout << a << endl; # cout << b0 << endl; # cout << c << endl; # cout << d << endl; # cout << e << endl; # }.data a:.word 0 b0:.word 1 c:.word 2 d:.word 3 e:.word 4 endl:.asciiz "\n".text main: lw $s0,e sw $s0,b0 lw lw add sw lw lw sub sw lw $s0,b0 $s1,c $t0,$s0,$s1 $t0,a $s0,b0 $s1,c $t0,$s0,$s1 $t0,d $s0,e page 2.59

sub sw $s0,$s0,1 $s0,e lw $a0,a # cout << a << endl; li $v0,1 la li $a0,endl $v0,4 lw $a0,b0 # cout << b0 << endl; li $v0,1 la li $a0,endl $v0,4 lw $a0,c # cout << c << endl; li $v0,1 la li $a0,endl $v0,4 lw $a0,d # cout << d << endl; li $v0,1 la li $a0,endl $v0,4 lw $a0,e # cout << e << endl; li $v0,1 la li li $a0,endl $v0,4 $v0,10 page 2.60

# # CSc 310 Example 2.2: First MAL program, variables in registers # Name: William Hsu # Date: 7/11/2000 # Description: Demonstrates simple MAL arithmetic instructions # # int a=0,b0=1,c=2,d=3,e=4; # # int main(void) # { # b0 = e; # a = b0 + c; # d = b0 - c; # e = e - 1; # # cout << a << endl; # cout << b0 << endl; # cout << c << endl; # cout << d << endl; # cout << e << endl; # } # a $s0 # b0 $s1 # c $s2 # d $s3 # e $s4 endl:.data.asciiz "\n".text main: li $s0,0 li $s1,1 li $s2,2 li $s3,3 li $s4,4 move $s1,$s4 # b = e; add $s0,$s1,$s2 # a = b + c; sub $s3,$s1,$s2 # d = b - c; sub $s4,$s4,1 # e = e - 1; move $a0,$s0 # cout << a << endl; li $v0,1 page 2.61

la li $a0,endl $v0,4 move $a0,$s1 # cout << b0 << endl; li $v0,1 la li $a0,endl $v0,4 move $a0,$s2 # cout << c << endl; li $v0,1 la li $a0,endl $v0,4 move $a0,$s3 # cout << d << endl; li $v0,1 la li $a0,endl $v0,4 move $a0,$s4 # cout << e << endl; li $v0,1 la li li $a0,endl $v0,4 $v0,10 page 2.62

# # CSc 310 Example 2.3: If statement, variables in memory # Name: William Hsu # Date: 7/11/2000 # Description: Demonstrates if statement translation # variables in memory # # #include <iostream.h> # int x=4,y=-1,sum=0; # # int main() # { # if (x > y) # sum = sum + x; # # cout << sum << endl; # }.data x:.word 4 y:.word -1 sum:.word 0 endl:.asciiz "\n".text main: lw $s0,x lw $s1,y lw $s2,sum ble $s0,$s1,skip # if (x > y) add $s2,$s2,$s0 # sum = sum + x; skip: sw $s2,sum move $a0,$s2 # cout << sum << endl; li $v0,1 la li li $a0,endl $v0,4 $v0,10 page 2.63

# # CSc 310 Example 2.4: If statement, variables in registers # Name: William Hsu # Date: 7/11/2000 # Description: Demonstrates if statement translation # # # #include <iostream.h> # int x=4,y=-1,sum=0; # # int main() # { # if (x > y) # sum = sum + x; # # cout << sum << endl; # } # x $s0 # y $s1 # sum $s2 endl:.data.asciiz "\n".text main: li $s0,4 li $s1,-1 li $s2,0 ble $s0,$s1,skip # if (x > y) add $s2,$s2,$s0 # sum = sum + x; skip: move $a0,$s2 # cout << sum << endl; li $v0,1 la li li $a0,endl $v0,4 $v0,10 page 2.64

# # CSc 310 Example 2.5: If-else statement, variables in registers # Name: William Hsu # Date: 7/11/2000 # Description: Demonstrates nested if-else statement # # # int main(void) # { # int num, flag; # # cin >> num; # # if (num > 0) # flag = 1; # else if (num < 0) # flag = -1; # else flag = 0; # # cout << flag << endl; # } # num $s0 # flag $s1 endl:.data.asciiz "\n".text main: li $v0,5 # cin >> num; move $s0,$v0 blez $s0,next1 # if (num > 0) li $s1,1 # flag = 1; b cont next1: bgez $s0,next2 # else if (num < 0) li $s1,-1 # flag = -1; b cont next2: li $s1,0 # else flag = 0; cont: move $a0,$s1 # cout << flag << endl; li $v0,1 la $a0,endl page 2.65

li li $v0,4 $v0,10 page 2.66

# # CSc 310 Example 2.6: For loop # Name: William Hsu # Date: 7/11/2000 # Description: User enters limit. Program computes sum of # integers from 1 to limit, and prints the sum. # # #include <iostream.h> # int main(void) # { # int sum=0,i,limit; # # cin >> limit; # for (i=1; i<=limit; i++) # sum = sum + i; # # cout << sum << endl; # # } # sum $s0 # i $s1 # limit $s2 endl:.data.asciiz "\n".text main: li $v0,5 # cin >> limit; move $s2,$v0 li $s0,0 li $s1,1 # for (i=1; i<=limit; i++) bgt $s1,$s2,cont loop: add $s0,$s0,$s1 # sum = sum + i; add $s1,$s1,1 ble $s1,$s2,loop cont: move $a0,$s0 # cout << sum << endl; li $v0,1 la li $a0,endl $v0,4 page 2.67

li $v0,10 page 2.68

ASCII Codes ASCII char ASCII char ASCII char code code code (hex) (hex) (hex) 0 nul 7 bel a nl d cr 20 sp 40 @ 60 ` 21! 41 A 61 a 22 " 42 B 62 b 23 # 43 C 63 c 24 $ 44 D 64 d 25 % 45 E 65 e 26 & 46 F 66 f 27 ' 47 G 67 g 28 ( 48 H 68 h 29 ) 49 I 69 i 2a * 4a J 6a j 2b + 4b K 6b k 2c, 4c L 6c l 2d - 4d M 6d m 2e. 4e N 6e n 2f / 4f O 6f o 30 0 50 P 70 p 31 1 51 Q 71 q 32 2 52 R 72 r 33 3 53 S 73 s 34 4 54 T 74 t 35 5 55 U 75 u 36 6 56 V 76 v 37 7 57 W 77 w 38 8 58 X 78 x 39 9 59 Y 79 y 3a : 5a Z 7a z 3b ; 5b [ 7b { 3c < 5c \ 7c 3d = 5d ] 7d } 3e > 5e ^ 7e ~ 3f? 5f _ 7f del page 2.69