CS代考计算机代写 assembler compiler assembly Java mips COMP273 McGill

COMP273 McGill
1
Assembler Arithmetic and Memory Access

Overview
• VariablesinAssembly
• AdditionandSubtractioninAssembly • Memory Access in Assembly
COMP273 McGill 2

Below Your Program • High-level language program (in C)
swap (int v[], int k) { int temp = v[k]; v[k] = v[k+1]; v[k+1] = temp;
}
• Assembly language program (for MIPS) swap: sll $2, $5, 2 add $2, $4,$2
C compiler
lw $15, lw $16, sw $16, sw $15, jr $31
• Machine (object) code (for MIPS)
0($2) 4($2) 0($2) 4($2)
assembler
000000 00000 00101 0001000010000000
000000 00100 00010 0001000000100000 …

Operators / Operands in High-level Languages
Operators: +, -, *, /, % ;
• 7/4==1, 7%4==3
Operands:
• Variables: fahr, celsius
• Constants: 0, 1000, -17, 15.4
Statement: Variable = Expression ;
• celsius = 5*(fahr-32)/9; • a = b+c+d-e;
COMP273 McGill
4

Assembly Design: Key Concepts • Assemblylanguageisdirectlysupportedinhardware
• It is kept very simple!
– Limit on the type of operands
– Limit the set of operations to absolute minimum
COMP273 McGill 5

The MIPS Instruction Set
MIPS Technology
MARS: Free MIPS Simulator
How do I learn MIPS assembly?
• Microprocessor without Interlocked Pipelined Stages (MIPS) • Used MIPS32 as the example in this course (Quickguide)
• Download the software
• Run the software java –jar pMARS.jar
• Try it out with MARS!
COMP273 McGill
6

Assembly Variables: Register
7

Assembly Variables: Registers
C and Java
• Operands are variables and
constants
• Declareasmanyasyouwant
MIPS
• Variablesarereplaced
by registers
• Operations can only
be performed on these!
• Limited number built directly into the hardware
COMP273 McGill
8
Why? Keep the Hardware Simple!

Assembly Variables: Registers
• MIPS has a register file of 32 registers
• Why 32? Smaller is faster​
• Each MIPS register is 32 bits = 4 bytes = a word​
Register File
4-bit (General) Register
COMP273 McGill From previous lecture on “Register and Memory” 9

COMP273 McGill
10
Assembly Variables: Registers
Good
Register file is small and inside of the core, so they are very fast
Bad
Since registers are implemented in the hardware, there are a predetermined number of them
MIPS code must be very carefully put together to efficiently use registers

Assembly Variables: Registers • Registers are numbered from 0 to 31
$0, $1, $2, … $30, $31
• Each register also has a name to make it easier to code: $16-$23 ➔ $s0-$s7
(s correspond to saved temporary variables)
$8 – $15 ➔ $t0 – $t7
(t correspond to temporary variables)
We will come back to s and t when we
talk about “procedure”
In general, use register names to make your code more readable
COMP273 McGill 11

Assembly Variables: Registers
$1, $26, $27 are reserved for assembler and operation system
COMP273 McGill 12

Comments
Assembly code is hard to read!
Another way to make your code more readable: comments!
C and Java
/* comment can span many lines */ // comment, to the end of a line
MIPS
# Anything from hash mark to end of line is a comment and will be ignored
COMP273 McGill 13

Assembly Instructions
C and Java
Each statement could represent multiple operations
a=b+c-d;
Is equivalent to two small operations
a=b+c; a=a–d;
MIPS
Each statement
(called an Instruction), executes exactly one of a short list of simple commands
COMP273 McGill
14

Addition and Subtraction
15

Addition and Subtraction
• Syntax of Instructions:
Operation Destination,Source1,Source2 Operation: by name (Mnemonic)
Destination: operand getting result
Source1: 1st operand for operation
Source2: 2nd operand for operation
• Syntax is rigid:
– Most of them use 1 operator + 3 operands (commas are optional) – Why? Keep Hardware simple via regularity
COMP273 McGill 16

Addition and Subtraction
Addition
Try with Mars
// C and Java
a=b+c;
# MIPS
add $s0 $s1 $s2
registers $s0,$s1,$s2 are associated with variables a, b, c Subtraction
// C and Java
d=e-f;
# MIPS
sub $s3,$s4,$s5
COMP273 McGill
17
registers $s3,$s4,$s5 are associated with variables d, e, f

Addition and Subtraction
Each Instruction, executes exactly one simple commands
C and Java
a = b + c + d – e;
MIPS
add $s0, $s1, $s2 #a=b+c
add $s0, $s0, $s3 #a=a+d
sub $s0, $s0, $s4 #a=a-e
COMP273 McGill
Break into
multiple instructions
A single line of C may break up into several lines of MIPS.
18

Immediates
• Immediatesarenumericalconstants.
• Specialinstructionsforimmediates:addi
• Syntax is similar to add instruction, except that last argument is a number (decimal or hexadecimal) instead of a register.
// C and Java
f = g + 10 ;
# MIPS
addi $s0 $s1 10 addi $s0 $s1 -10
• There is no subi (use a negative immediate instead)
COMP273 McGill 19

Register Zero
• MIPSdefinesregisterzero($0or$zero)alwaysbe0. • The number zero appears very often in code.
• Use this register, it’s very handy!
add $6$0$5 #copy$5to$6 addi $6 $0 77 # copy 77 to $6
• Register zero cannot be overwritten
COMP273 McGill 20
addi $0 $0 5 # will do nothing

Register Zero • What if you want to negate a number?
sub $6 $0 $5 # $6 = 0 – $5
COMP273 McGill 21

Data Transfer Instructions
29

Data Transfer Instructions
• MIPSarithmeticinstructionsonlyoperateonregisters • Whataboutlargedatastructureslikearrays?Memory!
– Add two numbers in memory
• Load values from memory into registers • Store result from register to memory
• UseDatatransferinstructionstotransferdatabetween registers and memory. We need to specify
– Register: specify this by number (0 – 31) – Memory address: more difficult
COMP273 McGill 30

Memory Address
Memory is a linear array of byte
Each byte in the memory has its own unique address
We can access the content by supplying the memory address
The processor can read or write the content of the memory
COMP273 McGill
31

Memory Address
• MemoryAddressSyntax:Offset(AddrReg)
– AddrReg: A register which contains a pointer to a memory location – Offset: A numerical offset in bytes (optional)
8($t0)
# specifies the memory address in $t0 plus 8 bytes
• We might access a location with an offset from a base pointer • The resulting memory address is the sum of these two values
COMP273 McGill 32

Memory Address
4-bit address example
// An array of 8 integers in C/Java
int arr[8]={56,26,88,45,-45,77,98,13} ;
# Assume $s0 has the address 0x1000
0($s0) # 0x1000, to access arr[0] 4($s0) # 0x1004, to access arr[1]
COMP273 McGill
33

Data Transfer: Memory to Register
• Load Instruction Syntax: lw DstReg, Offset(AddrReg) – lw: Load a Word
– DstReg: register that will receive value
– Offset: numerical offset in bytes
– AddrReg: register containing pointer to memory
lw $t0, 8($s0)
# load one word from memory at
address stored in $s0 with an offset 8 and store the content in $t0
COMP273 McGill 34

Data Transfer: Register to Memory
• Storeinstructionsyntax:swDataReg,Offset(AddrReg) – sw: Store a word
– DstReg: register containing the data
– Offset: numerical offset in bytes
– AddrReg: register containing memory
sw $t0, 4($s0)
# Store one word (32 bits) to memory address $s0 + 4
COMP273 McGill 35

Byte vs. word
• Machines address memory as bytes
• Both lw and sw access one word at a time
• The sum of the base address and
the offset must be a multiple of 4 (to be word aligned)
sw $t0, 0($s0)
sw $t0, 4($s0)
sw $t0, 8($s0)
. .
COMP273 McGill 36

Byte vs. word
Index 8 requires offset of 32 Index 12 reuqires offset of 48
Try with Mars
// C and Java
A[12] = h + A[8] ;
# MIPS
# assume h is stored in $s0 and the base address of A is in $s1 lw $s2 32($s1) # load A[8] to $s2
add $s3 $s0, $s2 # $s3 = $s0 + $s2
sw $s3 48($s1) # store result to A[12]
COMP273 McGill
37

Register vs. Memory
Operations with registers are faster than memory
Why not keep all variables in memory?
What if more variables than registers?
• MIPS arithmetic instructions can read 2 registers, operate on them, and write 1 per instruction
• MIPS data transfer only read or write 1 operand per instruction, and no operation
• Smaller is faster
COMP273 McGill
38
• Compiler tries to keep most frequently used variable in registers
• Writing less common to memory: spilling

Pointers vs. Values
• A register can hold any 32-bit value. – a (signed) int,
–anunsigned int,
–a pointer (memory address),
– etc.
lw $t2, 0($t0) # $t0 must contain?
add $t3, $t4, $t5 # what can you say about $t4 and $t5?
COMP273 McGill 39

Registers:
Review and Information
• The variables in assembly
• Saved Temporary Variables, Temporary Variables, Register Zero
Instructions:
• Addition and Subtraction: add, addi, sub • Data Transfer: lw, sw
References
• Textbook: 2.1, 2.2, 2.3, A.10 • MARS Tutorial
COMP273 McGill
40