Top Banner
1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal [email protected] S16 #06-17 Adapted from D. Patterson’s CS61C http://www.cs.berkeley.edu/~pattrsn/61CF00 Copyright 2000 UCB
25

1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal [email protected] S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

1

CS1104 Assembly Language: Part 1

Dr. Ankush Mittal

[email protected]

S16 #06-17

Adapted from D. Patterson’s CS61C

http://www.cs.berkeley.edu/~pattrsn/61CF00

Copyright 2000 UCB

Page 2: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Computer Organization

I/O systemProcessor

CompilerOperating

System(Windows 98)

Application (Netscape)

Digital Design

Instruction Set Architecture

°Coordination of many levels of abstraction

Datapath & Control

MemoryHardware

Software Assembler

Page 3: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Levels of RepresentationHigh Level Language

Program (e.g., C)

Assembly Language Program (e.g.,MIPS)

Machine Language Program (MIPS)

Control Signal Specification

Compiler

Assembler

Machine Interpretation

temp = v[k];

v[k] = v[k+1];

v[k+1] = temp;lw $to,

0($2)lw $t1,

4($2)sw $t1,

0($2)sw $t0,

4($2)

0000 1001 1100 0110 1010 1111 0101 10001010 1111 0101 1000 0000 1001 1100 0110 1100 0110 1010 1111 0101 1000 0000 1001 0101 1000 0000 1001 1100 0110 1010 1111

°°

Page 4: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

MIPS

° Instruction set used by NEC, Nintendo, Silicon Graphics, Sony, etc.

°Typical of instruction sets designed since early 1980s

Page 5: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Overview

°C operators, operands

°Variables in Assembly: Registers

°Comments in Assembly

°Addition and Subtraction in Assembly

°Memory Access in Assembly

Page 6: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

C Operators/Operands (1/2)°Operators: +, -, *, /, % (mod);

•7/4==1, 7%4==3

°Operands: • Variables: lower, upper, fahr, celsius

• Constants: 0, 1000, -17, 15.4

°Assignment Statement:

Variable = expression• Examples:

celsius = 5*(fahr-32)/9;

a = b+c+d-e;

Page 7: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

C Operators/Operands (2/2)

° In C (and most High Level Languages) variables declared first and given a type

• Example: int fahr, celsius; char a, b, c, d, e;

°Each variable can ONLY represent a value of the type it was declared as (cannot mix and match int and char variables).

Page 8: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Assembly Design: Key Concepts°Keep it simple!

• Limit what can be a variable and what can’t

• Limit types of operations that can be done to absolute minimum

- if an operation can be decomposed into a simpler operation, don’t include it

Page 9: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Assembly Variables: Registers (1/4)°Unlike HLL, assembly cannot use variables

• Why not? Keep Hardware Simple

°Assembly Operands are registers• limited number of special locations built directly into the hardware

• operations can only be performed on these!

°Benefit: Since registers are directly in hardware, they are very fast

Page 10: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Assembly Variables: Registers (2/4)°Drawback: Since registers are in hardware, there are a predetermined number of them

• Solution: MIPS code must be very carefully put together to efficiently use registers

°32 registers in MIPS• Why 32? Smaller is faster

°Each MIPS register is 32 bits wide• Groups of 32 bits called a word in MIPS

Page 11: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Assembly Variables: Registers (3/4)°Registers are numbered from 0 to 31

°Each register can be referred to by number or name

°Number references:$0, $1, $2, … $30, $31

Page 12: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Assembly Variables: Registers (4/4)°By convention, each register also has a name to make it easier to code

°For now:$16 - $22 $s0 - $s7

(correspond to C variables)

$8 - $15 $t0 - $t7

(correspond to temporary variables)

° In general, use names to make your code more readable

Page 13: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Comments in Assembly°Another way to make your code more readable: comments!

°Hash (#) is used for MIPS comments• anything from hash mark to end of line is a comment and will be ignored

°Note: Different from C.• C comments have format /* comment */ , so they can span many lines

Page 14: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Assembly Instructions° In assembly language, each statement (called an Instruction), executes exactly one of a short list of simple commands

°Unlike in C (and most other High Level Languages), each line of assembly code contains at most 1 instruction

Page 15: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Addition and Subtraction (1/4)°Syntax of Instructions:

1 2,3,4

where:

1) operation by name

2) operand getting result (“destination”)

3) 1st operand for operation (“source1”)

4) 2nd operand for operation (“source2”)

°Syntax is rigid:• 1 operator, 3 operands

• Why? Keep Hardware simple via regularity

Page 16: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Addition and Subtraction (2/4)°Addition in Assembly

• Example: add $s0,$s1,$s2 (in MIPS)

Equivalent to: a = b + c (in C)

where registers $s0,$s1,$s2 are associated with variables a, b, c

°Subtraction in Assembly• Example: sub $s3,$s4,$s5 (in MIPS)

Equivalent to: d = e - f (in C)

where registers $s3,$s4,$s5 are associated with variables d, e, f

Page 17: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Addition and Subtraction (3/4)°How do the following C statement?

a = b + c + d - e;

°Break into multiple instructionsadd $s0, $s1, $s2 # a = b + c

add $s0, $s0, $s3 # a = a + d

sub $s0, $s0, $s4 # a = a - e

°Notice: A single line of C may break up into several lines of MIPS.

°Notice: Everything after the hash mark on each line is ignored (comments)

Page 18: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Addition and Subtraction (4/4)°How do we do this?

•f = (g + h) - (i + j);

°Use intermediate temporary registeradd $s0,$s1,$s2 # f = g + h

add $t0,$s3,$s4 # t0 = i + j

# need to save i+j, but can’t use # f, so use t0

sub $s0,$s0,$t0 # f=(g+h)-(i+j)

Page 19: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Role of Registers vs. Memory°What if more variables than registers?

• Compiler tries to keep most frequently used variable in registers

• Writing less common to memory: spilling

°Why not keep all variables in memory?• Smaller is faster:registers are faster than memory

• Registers more versatile: - MIPS arithmetic instructions can read 2,

operate on them, and write 1 per instruction

- MIPS data transfer only read or write 1 operand per instruction, and no operation

Page 20: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

So Far...°All instructions have allowed us to manipulate data.

°So we’ve built a calculator.

°In order to build a computer, we need ability to make decisions…

Page 21: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Overview°C/Assembly Decisions: if, if-else

°C/Assembly Loops: while, do while, for

°Inequalities

°C Switch Statement

Page 22: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

C Decisions: if Statements

°2 kinds of if statements in C•if (condition) clause•if (condition) clause1 else clause2

°Rearrange 2nd if into following:if (condition) goto L1; clause2; go to L2;L1: clause1;

L2:

• Not as elegant as if-else, but same meaning

Page 23: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

MIPS Decision Instructions

°Decision instruction in MIPS:•beq register1, register2, L1•beq is “Branch if (registers are) equal” Same meaning as (using C): if (register1==register2) goto L1

°Complementary MIPS decision instruction•bne register1, register2, L1•bne is “Branch if (registers are) not equal” Same meaning as (using C): if (register1!=register2) goto L1

°Called conditional branches

Page 24: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

MIPS Goto Instruction°In addition to conditional branches, MIPS has an unconditional branch:

j label

°Called a Jump Instruction: jump (or branch) directly to the given label without needing to satisfy any condition

°Same meaning as (using C): goto label

°Technically, it’s the same as:

beq $0,$0,label

since it always satisfies the condition.

Page 25: 1 CS1104 Assembly Language: Part 1 Dr. Ankush Mittal ankush@comp.nus.edu.sg S16 #06-17 Adapted from D. Patterson’s CS61C pattrsn/61CF00.

Things to Remember (1/2)

°A Decision allows us to decide which pieces of code to execute at run-time rather than at compile-time.

°C Decisions are made using conditional statements within an if, while, do while or for.

°MIPS Decision making instructions are the conditional branchesIn order to help the conditional branches make decisions concerning inequalities, we introduce a single instruction: “Set on Less Than”