Download Ch. 4 Variables and Expressions slides

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Ch. 4 Variables and Expressions
1
Comp Sci 251 -- vars & expns
Supporting high-level languages

How do we use MIPS assembly language to
implement
–
–
–
2
Variable declaration & initialization?
Assignment statements?
Expression evaluation?
Comp Sci 251 -- vars & expns
##
##
##
##
##
##
##
##
File: foo.a
Brief explanation of program's purpose
Author: Your name
Date: 10 February 2010
#######################################
#
Text segment
#
#######################################
.text
.globl __start
__start:
Program
structure
Note: file extension .a
[program instructions...]
li $v0, 10
syscall
#exit
########################################
#
Data segment
#
########################################
.data
[data definitions...]
3
## end of file foo.a
[blank line]
Comp Sci 251 -- vars & expns
syscalls
4
Service
Call code
Arguments
Result
Print_integer
1
$a0 = integer
Print_float
2
$f12 = float
Print_double
3
$f12, $f13 = double
Print_string
4
$a0 = address of string
Read_int
5
$v0 (integer)
Read_float
6
$f0 (float)
Read_double
7
$f0, $f1 (double)
Read_string
8
Exit
10
Print_char
11
Read_char
12
$a0 = buffer, $a1 = length
$a0 = char
$v0 (char)
Comp Sci 251 -- vars & expns
Variable declaration

Use assembler directives
–
–
–

5
Not machine instructions
Reserve memory
Define symbols
Affect data segment of memory
Comp Sci 251 -- vars & expns
Variable declaration
High-level
int x;
MIPS assembly
x: .space 4
Symbol name
Data type
6
No. of bytes
Reserve
memory
Comp Sci 251 -- vars & expns
Sequence of declarations
Problem!!!
char x;
int y;
char z;
x:
y:
z:
7
.data
.space 1
.space 4
.space 1
0x10000000
x
0x10000001
y
0x10000005
z
Comp Sci 251 -- vars & expns
Alignment


8
Integer variables must be “word-aligned”
Use the .align directive

Syntax:
.align n

Semantics: assembler aligns the next
reserved location on an address divisible by
2n
Comp Sci 251 -- vars & expns
Example
x:
y:
z:
9
.data
.space
.align
.space
.space
1
2
4
1
0x10000000
x
0x10000004
y
0x10000008
z
Comp Sci 251 -- vars & expns
Initialization
int x = 5;
x:
initial value
.word 5
size
symbol name
10
Comp Sci 251 -- vars & expns
.word n


Reserves & initializes a word of memory
n can be
–
–
–

Automatically aligns to word boundary
–
11
Unsigned number
Signed number
Hexadecimal number
Unnecessary to use .align directive before
.word
Comp Sci 251 -- vars & expns
Byte order

We will use SPIM
–
–



12
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
Example
x:
.word 5
Little Endian
00
13
x
05
00
00
00
00
05
00
x
Comp Sci 251 -- vars & expns
What about character data?
char x = 'a';
14
x:
.byte 'a'
Comp Sci 251 -- vars & expns
.byte n


Reserves & initializes a byte of memory
n can be
–
–
–
–
15
Unsigned number
Signed number
Hexadecimal number
Character in single quotes  ASCII code
Comp Sci 251 -- vars & expns
Strings
char s[] = "hello";
s:
.asciiz "hello"
Cstring variable
Array of char
(Null-terminated)
16
Comp Sci 251 -- vars & expns
.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.
–
–
17
Leftmost char  lowest address
Rightmost char  highest address
Comp Sci 251 -- vars & expns
Example
x:
.asciiz "hello"
0x68
x
0x65
0x6c
0x6c
See Chapter03/data.a
0x6f
0x00
18
Comp Sci 251 -- vars & expns
Assignment



19
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
sw $t0, x
x:
20
#load immediate
#store word
.data
.space 4
Comp Sci 251 -- vars & expns
Load immediate instruction
li reg, value


value is loaded into register
Value is part of the instruction
–
–
21
not contained in data segment
not contained in register
Comp Sci 251 -- vars & expns
Store word instruction
sw reg, address



register contents are copied into memory
address can be a symbol or a number
address must be word-aligned
–
22
otherwise exception is raised
Comp Sci 251 -- vars & expns
Load/Store architecture


MIPS is a Reduced Instruction Set Computer
(RISC)
Philosophy: superior performance through
–
–
–

Some operations require several instructions
–
23
simple instructions
small instruction set
fast instructions
assignment requires load & store
Comp Sci 251 -- vars & expns
Assignment of char data
char y;
y = 'a';
.text
li $t0, 'a'
sb $t0, y
y:
24
#MSBs of $t0=0
#store byte
.data
.space 1
Comp Sci 251 -- vars & expns
Store byte instruction
sb reg, address

25
low-order byte of register is copied into
memory
Comp Sci 251 -- vars & expns
Assignment between variables
int x;
int y = 5;
.text
lw $t0, y
sw $t0, x
#store word
x = y;
x:
y:
26
.data
.space 4
.word 5
Comp Sci 251 -- vars & expns
Load word instruction
lw reg, address



27
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
a:
b1:
28
.data
.space 1
.byte '@’
#b assembly
# error
Comp Sci 251 -- vars & expns
Load byte unsigned instruction
lbu reg, address


29
byte of memory is copied into LSB of register
MSBs are cleared (= 0)
Comp Sci 251 -- vars & expns
Exercise


Write equivalent MIPS code
Sketch memory layout
int x = 25;
char a = '*';
int y;
char b;
y = x;
b = a;
30
Comp Sci 251 -- vars & expns
Arithmetic expressions

High level language feature

How do we evaluate expressions in
assembly language?
–
–
31
Single operator
Multiple operators
Comp Sci 251 -- vars & expns
Addition
Expression
MIPS code
2 + 3
li $t1, 2
li $t2, 3
add $t0, $t1, $t2
Goal: result in $t0
32
Comp Sci 251 -- vars & expns
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
33
Comp Sci 251 -- vars & expns
Maximize register re-use
Expression
MIPS code
2 + 3
li $t0, 2
li $t1, 3
add $t0, $t0, $t1
Goal: result in $t0
34
Comp Sci 251 -- vars & expns
Subtraction
35
Expression
MIPS code
2 - 3
li $t0, 2
li $t1, 3
sub $t0, $t0, $t1
Comp Sci 251 -- vars & expns
Sub instruction
sub rd, rs, rt



36
All operands must be registers
rd  rs - rt
Signed overflow  exception is raised
Comp Sci 251 -- vars & expns
Multiplication
37
Expression
MIPS code
2 * 3
li $t0, 2
li $t1, 3
mul $t0, $t0, $t1
Comp Sci 251 -- vars & expns
Mul instruction
mul rd, rs, rt (pseudo instruction)





38
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
39
Expression
MIPS code
2 / 3
li $t0, 2
li $t1, 3
div $t0, $t0, $t1
Comp Sci 251 -- vars & expns
Div instruction
div rd, rs, rt




40
Signed division
All operands must be registers
rd  rs / rt
Signed overflow  exception is raised
Comp Sci 251 -- vars & expns
Remainder (% operator)
41
Expression
MIPS code
2 % 3
li $t0, 2
li $t1, 3
rem $t0, $t0, $t1
Comp Sci 251 -- vars & expns
Rem instruction
rem rd, rs, rt



42
For simplicity, stick to non-negative operands
All operands must be registers
rd  rs % rt
Comp Sci 251 -- vars & expns
Multi-operator expressions
(1 + 2) * (3 – 4)

Order of operations depends on
–
–
–

43
Precedence rules
Associativity
Parentheses
Several orders are possible
+ - *
- + *
Comp Sci 251 -- vars & expns
Left-to-right evaluation method



Read expression left to right
Constant or variable  lowest unused t-reg
Operator
–
–
–
44
Wait until both operands in registers
Perform operation
Result  left operand register
Comp Sci 251 -- vars & expns
Exercise
Apply left-to-right method to
(1 + 2) * (3 – 4)
45
Comp Sci 251 -- vars & expns
Optimization

Sometimes you can do better than l-t-r
–
–

Advanced topics
–
–
46
Fewer registers
Fewer instructions
Sethi-Ullman numbering (minimize registers)
Common subexpressions (minimize instructions)
Comp Sci 251 -- vars & expns
Optimization exercise
x + y * z – y



47
L-t-r evaluation code
Minimize number of registers
Minimize number of instructions
Comp Sci 251 -- vars & expns
Related documents