CS代考 CSE12 Assembly II

CSE12 Assembly II

• Lab3 next week
• Missing labs

Copyright By PowCoder代写 加微信 powcoder

•Remember to attend one lab a week
•Mandatory attendance, some of you miss labs •3+ missing labs may mean you fail the class
Annoucements
Prof. Renau

•Basic programming constructs •sequence statements
Last (prev-midterm) class
CMPE 110 Prof. Renau

• while •do while • for
•6.2-Assembly directives
•6.1-Assembly code snippets • if/else
Prof. Renau

• Sequential Blocks • IF/ELSE
• While loops
• Do while loops
• For loops
Basic Structures
Prof. Renau

IF/ELSE Structure
if condition
taken_stmts
not_taken_stmts
TF condition
taken_stmts
not_taken_stmts
Prof. Renau

IF/ELSE Example
money=money+3 money=money-1
if money > 30
money = money – 1
money = money + 3
Prof. Renau

IF/ELSE Translation
Money>30? money=money+3 money=money-1
if money > 30
money = money – 1
money = money + 3
b?? goelse_label
// money = money – 1
jal x0, doneif_label
goelse_label:
// money = money + 3
doneif_label:
Prof. Renau

IF/ELSE Assembly
Money>30? money=money+3 money=money-1
b?? goelse_label
// money = money – 1
jal x0, doneif_label
goelse_label:
// money = money + 3
doneif_label:
// x3 address for money
// x10 money contents
lw x10, 0(x3)
addi x2, x0, 30
blt x10, x2, 12
addi x10, x10, -1
addi x10, x10, 3
sw x10, 0(x3)
Prof. Renau

Share notes and comments with the people next to you. You can prepare a question for the end of the break.
2 MINUTE THINKING BREAK
CMPE 110 Prof. Renau

While Loop Structure
T loop_body
while test
Prof. Renau

while money > 20
interest = interest + 1
money = money-interest
T loop_body
While Loop Example
Prof. Renau

while money > 20
interest = interest + 1
doloop: // Perform condition check b?? x??,x?? L1_done // money <= 20 // interest = interest + 1 // money = money-interest jal x0, doloop = money-interest While Loop Translation Prof. Renau while money > 20
interest = interest + 1
money = money-interest
// interest @ 0x54
// money @ 0x50
// x3 address for money
// x10 money contents
// x4 addr interest
addi x2, x0, 20
lw x10, 0(x3)
bge x2, x10, 28 // 20>=m
lw x5, 4(x3)
addi x5, x5, 1
sw x5, 4(x3)
sub x10, x10, x5
sw x10, 0(x3)
jal x0, -24
doloop: // Perform condition check b?? x??,x?? L1_done_label
// interest = interest + 1
// money = money-interest
jal x0, doloop
L1_done_label:
While Loop Assembly
Prof. Renau

// Sum integers from 1 to 100
while count <= 100 sum = sum + count Pseudo-Code Prof. Renau Share notes and comments with the people next to you. You can prepare a question for the end of the break. 2 MINUTE THINKING BREAK CMPE 110 Prof. Renau while condition total = 1000 sum =0 total = total + 1 sum = sum + 1 while total < 100 Do While Loop Structure Prof. Renau Share notes and comments with the people next to you. You can prepare a question for the end of the break. 2 MINUTE THINKING BREAK CMPE 110 Prof. Renau T loop_body For Loop Structure for init ; test ; re-init Prof. Renau for i=0 to 100 money = money + i for i=0 ; i<100 ; i++ money = money + i T loop_body For Loop Example Prof. Renau for i=0 to 100 money = money + i // init: i=0 doloop: // Perform condition check b?? x??, x??, L1_done_label // money = money + i // step: i=i+1 jal x0, doloop L1_done_label: For Loop Translation Prof. Renau Share notes and comments with the people next to you. You can prepare a question for the end of the break. 2 MINUTE THINKING BREAK CMPE 110 Prof. Renau for i=0 to 100 if money > 44
money = money + 2
foo = foo + 3
Nesting Constructs Translation
Prof. Renau

Nesting Constructs Translation
for i=0 to 100
if money > 44
money = money + 2
foo = foo + 3
// init: i = 0
doloop:// Perform condition check
b?? x?, x?, loop_done
b?? x?, x?, goelse_label // money = money + 2 jal x0, doneif_label
goelse_label:
// foo = foo + 3
doneif_label:
// step: i = i + 1
jal x0, doloop
loop_done:
Prof. Renau

Share notes and comments with the people next to you. You can prepare a question for the end of the break.
2 MINUTE THINKING BREAK
CMPE 110 Prof. Renau

Programming Flow
High Level Language
Compiler Assembly Assembler Language
Machine Language
Compiler: A computer program that translates code written in a high level language into an intermediate level abstract language.
Assembler: A computer program that translates code written in assembly language to the binary form that the CPU can execute.
CMPE 110 Prof. Renau

Compiling Assembly
• Compile assembly to machine code
riscv64-unknown-elf-gcc -march=rv32i -mabi=ilp32 -c -o sequential.o sequential.s
• Generate an hex file out of machine code riscv64-unknown-elf-objcopy -O ihex sequential.o sequential.hex
# Sequential code example
addi x2, x2, 80
lw x3, 0(x2)
addi x3, x3, 3
addi x4, x0, 84
lw x5, 0(x4)
sub x3, x3, x5
sw x3, 0(x2)
jal x0, 0
Prof. Renau

• Anything after a #
Assembly Comments
# Sequential code example
addi x2, x2, 80
lw x3, 0(x2)
addi x3, x3, 3 # also comment
addi x4, x0, 84
lw x5, 0(x4)
sub x3, x3, x5
sw x3, 0(x2)
jal x0, 0
Prof. Renau

• identifier followed by a colon foo:
# Sequential code example
addi x2, x2, 80
lw x3, 0(x2)
addi x3, x3, 3 # also comment
addi x4, x0, 84
lw x5, 0(x4)
sub x3, x3, x5
sw x3, 0(x2)
my_end_of_code:
jal x0, my_end_of_code
jal x0, label3
Assembly Labels
Prof. Renau

• period and command .byte
.global __reset
lw x3, money
addix3,x3,3 #money=money+3
lw x5, interest
sub x3, x3, x5 # money = money – interest sw x3, money, x8
my_end_of_code:
jal x0, my_end_of_code
.word 33 # initialized to 33
.word 2 # initialized to 2
Assembly Directives
Prof. Renau

Assembly RISC-V Pseudo-instructions
• Syntax sugar
•Assembler translates for you
• RISC-V spec
• https://riscv.org/wp-content/uploads/2017/05/riscv-spec-v2.2.pdf •page 110
CMPE 110 Prof. Renau

Elements of Assembly Language
# This is a comment
.global __reset
• Instructions
•risc-v (same as before with help) •risc-v pseudo-instructions
• Comments (#…) • Labels
•The compiler help to compute immediates • Assembler Directives
• https://michaeljclark.github.io/asm.html
# call C main
j irq_handler
irq_handler:
la gp, __global_pointer
la sp, __stack_pointer
la t0, __bss_start
la t1, __bss_end
bgeu t0, t1, memclr_done
sw zero, (t0)
addi t0, t0, 4
bltu t0, t1, memclr
memclr_done:
Prof. Renau

• Linux binaries have 3 main sections •Most OS have similar systems
•Read only data or program binary
•Increases binary size • DATA
•Read/Write data that can be initialized to some value
•Increases binary size • BSS
•Read/Write data initialized to zero
•Allocated by OS, it does not increase binary size •An extra load may be needed to get BSS starting point
• Since we do not have OS, in emulsiv, DATA is similar to BSS •Just that “someone” has to clear to zero
Review on Binary Sections
CMPE 110 Prof. Renau

•WEEK 7-8 •Code snippets
CMPE 110 Prof. Renau

•6.3-compiling RISC-V baremetal •6.4-C vs assembly
Next Class
Prof. Renau

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com