ch 4 variables and expressions
Download
Skip this Video
Download Presentation
Ch. 4 Variables and Expressions

Loading in 2 Seconds...

play fullscreen
1 / 47

Ch. 4 Variables and Expressions - PowerPoint PPT Presentation


  • 75 Views
  • Uploaded on

Ch. 4 Variables and Expressions. Supporting high-level languages. How do we use MIPS assembly language to implement Variable declaration & initialization? Assignment statements? Expression evaluation?. Program structure. ## ## File: foo.a ## ## Brief explanation of program\'s purpose ##

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Ch. 4 Variables and Expressions' - joshwa


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
ch 4 variables and expressions

Ch. 4 Variables and Expressions

Comp Sci 251 -- vars & expns

supporting high level languages
Supporting high-level languages
  • How do we use MIPS assembly language to implement
    • Variable declaration & initialization?
    • Assignment statements?
    • Expression evaluation?

Comp Sci 251 -- vars & expns

program structure
Program structure

##

## File: foo.a

##

## Brief explanation of program\'s purpose

##

## Author: Your name

## Date: 10 February 2010

##

#######################################

# Text segment #

#######################################

.text

.globl __start

__start:

[program instructions...]

li $v0, 10 #exit

syscall

########################################

# Data segment #

########################################

.data

[data definitions...]

## end of file foo.a

[blank line]

Note: file extension .a

Comp Sci 251 -- vars & expns

syscalls
syscalls

Comp Sci 251 -- vars & expns

variable declaration
Variable declaration
  • Use assembler directives
    • Not machine instructions
    • Reserve memory
    • Define symbols
  • Affect data segment of memory

Comp Sci 251 -- vars & expns

variable declaration1
Variable declaration

High-level

int x;

MIPS assembly

x: .space 4

Symbol name

No. of bytes

Reserve

memory

Data type

Comp Sci 251 -- vars & expns

sequence of declarations problem
char x;

int y;

char z;

.data

x: .space 1

y: .space 4

z: .space 1

Sequence of declarations Problem!!!

Comp Sci 251 -- vars & expns

alignment
Alignment
  • Integer variables must be “word-aligned”
  • Use the .align directive
  • Syntax:

.alignn

  • Semantics: assembler aligns the next reserved location on an address divisible by 2n

Comp Sci 251 -- vars & expns

example
Example

.data

x: .space 1

.align 2

y: .space 4

z: .space 1

Comp Sci 251 -- vars & expns

initialization
int x = 5;

x: .word 5

Initialization

initial value

size

symbol name

Comp Sci 251 -- vars & expns

word n
.word n
  • Reserves & initializes a word of memory
  • n can be
    • Unsigned number
    • Signed number
    • Hexadecimal number
  • Automatically aligns to word boundary
    • Unnecessary to use .align directive before .word

Comp Sci 251 -- vars & expns

byte order
Byte order
  • We will use SPIM
    • MIPS simulator software
    • Runs on many different platforms
  • Byte order in SPIM depends on native byte order of platform
  • Intel: little-endian
  • PowerPC (Mac): big-endian

Comp Sci 251 -- vars & expns

example1
Example

x: .word 5

Little Endian

Comp Sci 251 -- vars & expns

what about character data
char x = \'a\';

x: .byte \'a\'

What about character data?

Comp Sci 251 -- vars & expns

byte n
.byte n
  • Reserves & initializes a byte of memory
  • n can be
    • Unsigned number
    • Signed number
    • Hexadecimal number
    • Character in single quotes  ASCII code

Comp Sci 251 -- vars & expns

strings
char s[] = "hello";

s: .asciiz "hello"

Strings

Cstring variable

Array of char

(Null-terminated)

Comp Sci 251 -- vars & expns

asciiz s
.asciiz s
  • S is a string in double quotes
  • Sequence of bytes is reserved & initialized
  • One byte per character
  • Final byte contains null character: 0x00
  • Note: not affected by byte order.
    • Leftmost char  lowest address
    • Rightmost char  highest address

Comp Sci 251 -- vars & expns

example2
Example

x: .asciiz "hello"

See Chapter03/data.a

Comp Sci 251 -- vars & expns

assignment
Assignment
  • Store a value in a variable
  • Occurs at runtime, not compile/assemble time
  • Supported with assembly language instructions

Comp Sci 251 -- vars & expns

simple assignment
int x;

x = 5;

.text

li $t0, 5 #load immediate

sw $t0, x #store word

.data

x: .space 4

Simple assignment

Comp Sci 251 -- vars & expns

load immediate instruction
Load immediate instruction

li reg, value

  • value is loaded into register
  • Value is part of the instruction
    • not contained in data segment
    • not contained in register

Comp Sci 251 -- vars & expns

store word instruction
Store word instruction

swreg, address

  • register contents are copied into memory
  • address can be a symbol or a number
  • address must be word-aligned
    • otherwise exception is raised

Comp Sci 251 -- vars & expns

load store architecture
Load/Store architecture
  • MIPS is a Reduced Instruction Set Computer (RISC)
  • Philosophy: superior performance through
    • simple instructions
    • small instruction set
    • fast instructions
  • Some operations require several instructions
    • assignment requires load & store

Comp Sci 251 -- vars & expns

assignment of char data
char y;

y = \'a\';

.text

li $t0, \'a\' #MSBs of $t0=0

sb $t0, y #store byte

.data

y: .space 1

Assignment of char data

Comp Sci 251 -- vars & expns

store byte instruction
Store byte instruction

sb reg, address

  • low-order byte of register is copied into memory

Comp Sci 251 -- vars & expns

assignment between variables
int x;

int y = 5;

x = y;

.text

lw $t0, y

sw $t0, x #store word

.data

x: .space 4

y: .word 5

Assignment between variables

Comp Sci 251 -- vars & expns

load word instruction
Load word instruction

lw reg, address

  • word of memory is copied into register
  • address must be word-aligned
  • Note: memory  memory transfer requires two instructions

Comp Sci 251 -- vars & expns

assignment between char variables
char a;

char b = \'@\';

a = b;

.text

lbu $t0, b1 #load byte

#unsigned

sb $t0, a

.data

a: .space 1

b1: .byte \'@’ #b assembly

# error

Assignment between char variables

Comp Sci 251 -- vars & expns

load byte unsigned instruction
Load byte unsigned instruction

lbu reg, address

  • byte of memory is copied into LSB of register
  • MSBs are cleared (= 0)

Comp Sci 251 -- vars & expns

exercise
Exercise
  • Write equivalent MIPS code
  • Sketch memory layout

int x = 25;

char a = \'*\';

int y;

char b;

y = x;

b = a;

Comp Sci 251 -- vars & expns

arithmetic expressions
Arithmetic expressions
  • High level language feature
  • How do we evaluate expressions in assembly language?
    • Single operator
    • Multiple operators

Comp Sci 251 -- vars & expns

addition
Expression

2 + 3

MIPS code

li $t1, 2

li $t2, 3

add $t0, $t1, $t2

Goal: result in $t0

Addition

Comp Sci 251 -- vars & expns

add instruction
Add instruction

add rd, rs, rt

  • All operands must be registers
  • First operand is destination
  • Second and third operands are sources
  • rd  rs + rt
  • Register may appear as source and destination
  • Signed overflow  exception is raised

Comp Sci 251 -- vars & expns

maximize register re use
Expression

2 + 3

MIPS code

li $t0, 2

li $t1, 3

add $t0, $t0, $t1

Goal: result in $t0

Maximize register re-use

Comp Sci 251 -- vars & expns

subtraction
Expression

2 - 3

MIPS code

li $t0, 2

li $t1, 3

sub $t0, $t0, $t1

Subtraction

Comp Sci 251 -- vars & expns

sub instruction
Sub instruction

sub rd, rs, rt

  • All operands must be registers
  • rd  rs - rt
  • Signed overflow  exception is raised

Comp Sci 251 -- vars & expns

multiplication
Expression

2 * 3

MIPS code

li $t0, 2

li $t1, 3

mul $t0, $t0, $t1

Multiplication

Comp Sci 251 -- vars & expns

mul instruction
Mul instruction

mul rd, rs, rt (pseudo instruction)

  • Signed multiplication
  • All operands must be registers
  • rd  rs * rt
  • No exception is raised on overflow. Why?
  • Equivalent to

mult rs, rt

mflo rd

Comp Sci 251 -- vars & expns

division
Expression

2 / 3

MIPS code

li $t0, 2

li $t1, 3

div $t0, $t0, $t1

Division

Comp Sci 251 -- vars & expns

div instruction
Div instruction

div rd, rs, rt

  • Signed division
  • All operands must be registers
  • rd  rs / rt
  • Signed overflow  exception is raised

Comp Sci 251 -- vars & expns

remainder operator
Expression

2 % 3

MIPS code

li $t0, 2

li $t1, 3

rem $t0, $t0, $t1

Remainder (% operator)

Comp Sci 251 -- vars & expns

rem instruction
Rem instruction

rem rd, rs, rt

  • For simplicity, stick to non-negative operands
  • All operands must be registers
  • rd  rs % rt

Comp Sci 251 -- vars & expns

multi operator expressions
Multi-operator expressions

(1 + 2) * (3 – 4)

  • Order of operations depends on
    • Precedence rules
    • Associativity
    • Parentheses
  • Several orders are possible

+ - *

- + *

Comp Sci 251 -- vars & expns

left to right evaluation method
Left-to-right evaluation method
  • Read expression left to right
  • Constant or variable  lowest unused t-reg
  • Operator
    • Wait until both operands in registers
    • Perform operation
    • Result  left operand register

Comp Sci 251 -- vars & expns

exercise1
Exercise

Apply left-to-right method to

(1 + 2) * (3 – 4)

Comp Sci 251 -- vars & expns

optimization
Optimization
  • Sometimes you can do better than l-t-r
    • Fewer registers
    • Fewer instructions
  • Advanced topics
    • Sethi-Ullman numbering (minimize registers)
    • Common subexpressions (minimize instructions)

Comp Sci 251 -- vars & expns

optimization exercise
Optimization exercise

x + y * z – y

  • L-t-r evaluation code
  • Minimize number of registers
  • Minimize number of instructions

Comp Sci 251 -- vars & expns

ad