CS计算机代考程序代写 assembler mips assembly Jumps vs Branches

Jumps vs Branches
■ Jump command takes you to the specified label
■ Jumps, however, do not do any comparisons
(they are unconditional)
CSE 12 W 2021
1

Jump region (J)
Address
8000 8004
……… ……… 12000
Instruction
J 12000;
Add $t4,$t4,$t5;
Addi $t5, $t3, 10;

J can jump to a label that is within the current region
◆ Main reason for having a separate Jump instruction instead of simply writing:
beq $0, $0, jump_label?
◆ Reason: separate jump instruction allows us to jump much farther than branch encoding.
Will be discussed more in advanced course CSE120; no need to worry about it now!
CSE 12 W 2021
2

Jump Example
Only 2 command executed:
j blah_label addi $t3, $0, 3
■ ■
CSE 12 W 2021
3

:
Jump Register (JR)
■ JR uses the instruction address in a register ◆ Example: JR $t0
◆ Full 32-bit address in register
■ JR instruction returns control to the caller. It copies the contents of $t0 into the PC (program counter), that keeps track of which instruction computer is currently executing
■ Usually you think of this as “jumping to the address contained in $t0.”
Address Instruction
8000 Addi $t2, 12000 8004 Jr $t2
………
………
12000 Addi $t5, $t3, 10
CSE 12 W 2021
4

Jumping (and Linking) : JAL
■ Some jumps can store the address of the following instruction
◆ Why?….Writing Functions (will discuss more in later slides! )
■ This is known as “linking”
◆ Similar behavior to J except next address (PC+4) is automatically
remembered in $ra (return address) ($r31) register
Address Instruction
8000 Jal 12000; // PC=8000, therefore ($ra)=PC+4 = 8004 8004 Add $t4, $t4,$t3;
………
………
12000 Addi $t5, $t3, 10 12004 Jr $ra
CSE 12 W 2021
5

Jump and Link Example in MARS
CSE 12 W 2021
6

Program Control Flow
■ You can conditionally execute sections of code using BEQ and B
■ Why do you need the B?
BEQ $t0, $t1, true_condition
CSE 12 W 2021
false_condition:
# statements
B end_condition
true_condition:
# statements
end_condition:
# after branch
7

Branching in a Loop
Tru e
False
CSE 12 W 2021
init:
li $t0, 0
loop_start:
beq $t0, 10, loop_end
loop_body:
move $a0, $t0
li $v0, 1
syscall update:
addi $t0, $t0, 1
b loop_start loop_end:
nop
8

Iterating over a String
CSE 12 W 2021
.text: init:
la $t0, hello_string loop_start:
lb $t2, ($t0)
beq $t2, $zero, loop_end loop_body:
move $a0, $t2 li $v0, 11
update: syscall
addi $t0, $t0, 1
b loop_start loop_end:
nop .data
hello_string:
.asciiz “Hello World!”
9

Pseudo Instructions (or Pseudo-ops)
■ The MIPS assembler contains many ”pseudo” instructions that look like instructions, but actually get mapped to other instructions.
◆ These are intended to save the programmer time and make code more readable (fewer instructions in written code)
◆ These remove “redundant” instructions (simpler processor)
CSE 12 W 2021
10

Pseudo-op: NOT
■ NOT can be implemented with NOR ◆ Remember: x nor 0 = !x
◆ Example:
★ NOT $t1, $t2
★ Gets mapped to: NOR $t1, $t2, $zero
CSE 12 W 2021
11

Pseudo-Op: Rotate
■ Rotate Left (ROL) and Rotate Right (ROR) are pseudo-ops
◆ Use SLL/SLLV and SRL/SRLV to generate intermediate results
◆ OR rotated bits back in ■ Example:
◆ ROL $t1, $t2, 3
■ Translates to:
◆ SRL $at, $t2, 29 ◆ SLL $t1, $t2, 3 ◆ OR $t1, $t1, $at
CSE 12 W 2021
12

Pseudo-op: Load address (recommended!)
■ LA $t1, label
◆ There is no la instruction in MIPS!
◆ Loads address of label into register: ★ LUI $t1, upper-16-bits
★ ORI $t1, lower-16-bits
CSE 12 W 2021
13

Pseudo-op: Load Immediate
■ Adding to zero is the same as load immediate
■ Can put 16-bit value in instruction itself Load
CSE 12 W 2021
Immediate Signed
◆ Example (using decimal notation): ★ LI $t1, 23
• This is actually ADDIU $t1, $zero, 23 ★ $t1 = 0 + 23
◆ Can use hex notation: ★ LI $t1, 0x0F
★ $t1 = 0x0F
◆ Can do negative immediate too
★ LI $t1, -23 is ADDI $t1, $zero, -23
14

Pseudo-op: Load 32-bit immediate
■ There is a 32-bit load-immediate pseudo-op too! ◆ Example:
★ LI$t1,0x1234FFFF ◆ Translates to:
★ LUI$t1,0x1234 ★ ORI$t1,0xFFFF
CSE 12 W 2021
15

Pseudo-op: Move
■ To initialize a register ◆ MOVE $t0, $zero
■ Translates to
◆ ADDU $t0, $zero, $zero
■ To move a register ◆ MOVE $t1, $t0
■ Translates to
◆ ADDU $t1, $t0, $zero
CSE 12 W 2021
16

Using MIPS in MARS
■ Registers are case sensitive
◆ $A0 is not $a0 (gives error)
■ Operations are case insensitive
◆ ADD is same as add
■ Use the symbolic register name like $t0
◆ Not $r1 but $1 in MARS (gives error)
◆ Recommend using $t0- $t7 for now… more later!
CSE 12 W 2021
17

System Call (syscall)
■ Calls special system code to do things like… ◆ Input from keyboard
◆ Output to screen
◆ Exit program
■ Code for system call is in register $v0
■ Arguments are (if needed) in $a0 and $a1
CSE 12 W 2021
18

Hello World!
.text
main:
li $v0,4
la $a0, greeting
syscall
li $v0, 10
syscall
.data
greeting:
.asciiz “Hello World”
terminated!).
# Define the program instructions.
# Label to define the main program.
# Load 4 into $v0 to indicate a
# print string.
# Load the address of the greeting
# into $a0.
# Print greeting. The print is
# indicated by $v0 having a value # of 4, and the string to print
# is stored at the address in $a0. # Load a 10 (halt) into $v0.
# The program ends.
# Define the program data.
#The string to print (null
CSE 12 W 2021
19

Conclusion
■ You should know the instructions presented here
■ There are some other less frequent ones
◆ You don’t need to remember them
◆ You should be able to understand them given the ISA information (e.g., the manual)!
CSE 12 W 2021
20

ayout
Data L
References:
1) MIPS_Vol2.pdf
2) Intro to MIPS Assembly Language Programming

Where does the data come from?
■ Data is arranged in memory
■ Data can be initialized when a program starts
(.data)
◆ Must be stored in the program with the instructions
■ Data may be not-initialized (or actually just zeroed) (.bss)
◆ This doesn’t need to be stored in the program itself
■ Data may be created during execution ◆ More later in the course!
CSE 12 W 2021
22

Code and Data Addresses
■ The code (text) segment starts at 0x0040_0000
◆ Each instruction is 4 bytes ■ The data segment starts at
0x1001_0000
◆ Data is placed at next adjacent location
■ Both grow “up”
■ What is the limit of my
program size?
■ What is the limit of my data size?
CSE 12 W 2021
23

MARS Data Layout Viewer: Hello World
■ Displayed as hexadecimal
CSE 12 W 2021
24

Data Declarations
.data # All of this will go in the “data” segment of memory var1: .word 3 # create a single integer variable with initial value 3
array1: .byte ‘a’,’b’ # create a 2-element character array # initialized to ASCII a and b
half1: .half 0x1234 # create 16-bit word
array2: .space 40 # allocate 40 consecutive bytes, with storage # uninitialized
string1: .asciiz “Hello!\n” # string variable with end null
string2: .ascii “Hello!\n” # string variable with NO end null
CSE 12 W 2021
25

Data Segment Layout (Packing)
■ Memory is arranged least significant byte first ◆ .align directive can force alignment
.data
var1: .word 3 array1: .byte ’a’,’b’
half1: .half 0x1234 string1:.asciiz “Hello!\n”
0x1001_0001
0x1001_0002
0x1001_0003
0x1001_0004
0x1001_0005
0x1001_0006
0x1001_0007
0x1001_0008
0x1001_0009
0x1001_000A
0x1001_000B
0x1001_000C
0x1001_000D
0x1001_000E
0x1001_000F
00
00
00
a
b
34
12
H
e
l
l
o
!
\n
CSE 12 W 2021
Address
0x1001_0000
Value
03
00
26

Computing Data Addresses
■ If data segment starts at 0x1001_0000…
◆ What is var1?
.data
var1: .word 3 array1: .byte ’a’,’b’
◆ What is array1?
◆ What is half1?
half1:
Label
var1
Address
0x1001_0000
Value
.half 0x1234
Spaces or new lines don’t matter!
CSE 12 W 2021
array1
half1
0x1001_0001
0x1001_0002
0x1001_0003
0x1001_0004
0x1001_0005
0x1001_0006
0x1001_0007
03
00
00
00
61 (a)
62 (b)
34
12
27

Arrays
■ Arrays are a sequence of identical elements
◆ Elements can be any size: byte, half word, word, or bigger
◆ Strings are an array of characters
■ Remember iterating over a sequence
CSE 12 W 2021
28

Iterating over a String (Array of Characters)
CSE 12 W 2021
.text: init:
la $t0, hello_string loop_start:
lb $t2, ($t0)
beq $t2, 0, loop_end loop_body:
move $a0, $t2 li $v0, 11
update: syscall
addi $t0, $t0, 1
b loop_start loop_end:
nop .data
hello_string:
.asciiz “Hello World!”
29

Array of Integers (4 bytes)
Address
Value
0x1001_0000
03
0x1001_0001
00
0x1001_0002
00
0x1001_0003
00
0x1001_0004
00
0x1001_0005
00
0x1001_0006
00
0x1001_0007
00
0x1001_0008
01
0x1001_0009
00
0x1001_000A
00
0x1001_000B
00
0x1001_000C
02
0x1001_000D
00
0x1001_000E
00
0x1001_000F
00
………..
……..
.data
list:
.word 3, 0, 1, 2, 6, -2, 7, 3, 7
.text
la $t3, list
li $t2, 3
sll $t2, $t2, 2
add $t1, $t2, $t3 address
lw $t4, 0($t1)
# put address of list into $t3
# put the index into $t2
# Multiple index by 4 (1 word is 4 bytes)
# combine the two parts of the
# get the value from the array cell
CSE 12 W 2021
30