CSE12 ASM/C
Lab3 this week lab4 soon
Must install WSL (Windows) or brew (OSX)
Copyright By Assignmentchef assignmentchef
Annoucements
Prof. Renau
6.1-Assembly code snippets if/else
6.2-Assembly directives
Last class
Prof. Renau
6.3-compiling RISC-V baremetal 6.4-C vs assembly
6.6-C-array in assembly 6.7-C-string in assembly
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)
subx3, x3, x5
sw x3, 0(x2)
jalx0, 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)
subx3, x3, x5
sw x3, 0(x2)
jalx0, 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)
subx3, x3, x5
sw x3, 0(x2)
my_end_of_code:
jalx0, my_end_of_code
jalx0, 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:
jalx0, my_end_of_code
.word 33 # initialized to 33
.word2 # 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
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
#include
int main() {
printf(hello world
);
Compiling Hello World
Prof. Renau
separated by spaces that does not start with a digit (0-9)
Variable declaration int potato;
char foo,bar; // not so nice coding wise, but legal
Variable Assignment potato = 3;
Variable
a continuous sequence of alphanumeric characters or underscores
CMPE 110 Prof. Renau
a continuous sequence of alphanumeric characters or underscores
separated by spaces that starts with a digit (0-9)
33 // Decimal 0x33 // Hexadecimal 012 // Octal
Numbers vs Variables
Variables
a continuous sequence of alphanumeric characters or underscores
separated by spaces that does not start with a digit (0-9)
CMPE 110 Prof. Renau
Arithmetic a = b+c
a = b*c a = c+3*4 a = 3+-3 /%
Bitwise a = b|c a = b^3
a = b & 0x3
a=b&&c //bandc :better: b!=0andc!=0 c=a||c //a!=0orc!=0
Expressions
CMPE 110 Prof. Renau
a += 3
Shorthand Expressions
Prof. Renau
b = a++ + a++ + a++;
What is the value of c?
What is the value of b?
Precedence
Prof. Renau
1. Left-to-right: (), aa++
2. Right-to-left: ++aa, -, +, !, ~
3. Left-to-right: *, /, %
4. Left-to-right: +,
5. Left-to-right: <<, >>
6. Left-to-right: <, <=, >, >=
7. Left-to-right: ==, !=
8. Left-to-right: &
9. Left-to-right: ^
10. Left-to-right: |
11. Left-to-right: &&
12. Left-to-right: ||
13. Right-to-left: +=, -=, ^=,
b = a++ + a++ + a++; // 1+2+3
Expressions Operator Precedence
Prof. Renau
1. Left-to-right: (), aa++
2. Right-to-left: ++aa, -, +, !, ~
3. Left-to-right: *, /, %
4. Left-to-right: +,
5. Left-to-right: <<, >>
6. Left-to-right: <, <=, >, >=
7. Left-to-right: ==, !=
8. Left-to-right: &
9. Left-to-right: ^
10. Left-to-right: |
11. Left-to-right: &&
12. Left-to-right: ||
13. Right-to-left: +=, -=, ^=,
b0=~a; //-2 b1 = ~a++; //
b2 = ~++a; //
b3 = ~(a+1);//
b4 = (~a)+1;//
Expressions Operator Precedence
Prof. Renau
1. Left-to-right: (), aa++
2. Right-to-left: ++aa, -, +, !, ~
3. Left-to-right: *, /, %
4. Left-to-right: +,
5. Left-to-right: <<, >>
6. Left-to-right: <, <=, >, >=
7. Left-to-right: ==, !=
8. Left-to-right: &
9. Left-to-right: ^
10. Left-to-right: |
11. Left-to-right: &&
12. Left-to-right: ||
13. Right-to-left: +=, -=, ^=,
b0=~a; //-2 b1 = ~a++; // -2 b2=~++a;//-3 b3 = ~(a+1);// -3 b4 = (~a)+1;// -1
Expressions Operator Precedence
Prof. Renau
1. Left-to-right: (), aa++
2. Right-to-left: ++aa, -, +, !, ~
3. Left-to-right: *, /, %
4. Left-to-right: +,
5. Left-to-right: <<, >>
6. Left-to-right: <, <=, >, >=
7. Left-to-right: ==, !=
8. Left-to-right: &
9. Left-to-right: ^
10. Left-to-right: |
11. Left-to-right: &&
12. Left-to-right: ||
13. Right-to-left: +=, -=, ^=,
b0 = 1 || 0 && 1; // b1 = !1 && 1; // b1 = ~1 && 1; // b1=~1 &1; // b1=!1 &1; //
Expressions Operator Precedence
Prof. Renau
1. Left-to-right: (), aa++
2. Right-to-left: ++aa, -, +, !, ~
3. Left-to-right: *, /, %
4. Left-to-right: +,
5. Left-to-right: <<, >>
6. Left-to-right: <, <=, >, >=
7. Left-to-right: ==, !=
8. Left-to-right: &
9. Left-to-right: ^
10. Left-to-right: |
11. Left-to-right: &&
12. Left-to-right: ||
13. Right-to-left: +=, -=, ^=,
b0 = 1 || 0 && 1; // 1 b1 = !1 && 1; //0 b1 = ~1 && 1; //1 b1=~1 &1; //0 b1=!1 &1; //0
Expressions Operator Precedence
Prof. Renau
Expressions Operator Precedence
Precedence can get tricky
My suggestion: DO NOT COMPLICATE YOUR LIFE
Unary higher precedence (!, ~) * more precedence than +
Use parenthesis otherwise
a || (b && c)
Do not use modifying operators in an expression.
Avoid things like: a = b++ + 3
CMPE 110 Prof. Renau
gcc or clang
https://learn.onlinegdb.com/
More for teaching C (no RISC-V)
https://godbolt.org/
Can generate risc-v assembly and useful
C compiler
Prof. Renau
int myfun(int a) {
if (a == 2) {
return 33;
return 44; }
Prof. Renau
#include
int main() {
int a = 1;
if (a == 2) {
printf(never_called
);
printf(hello world
);
Prof. Renau
#include
int main() {
int a = 1;
while (a < 3 ) { printf(“hello world
“);Prof. Renau #include
int main() {
for(int i=0;i<5;i++) { printf(“hello world
“);Prof. Renau #include
int fun1(int a) {
int b = a;
int a = 3;
printf(1. a=%d
,a);
printf(2. a=%d
,a);
int main() {
int c = fun1(a);
printf(3. a=%d c=%d
,a,c);
Variable Scope
Prof. Renau
C strings are zero terminated hello becomes h e l l o 0
#include
int main() {
char array[10] = hello;
char *ptr = hello;
printf(my string is %s
,array); printf(my string is %s
,ptr);
for(int i=0;i<8;i++) {printf(“[%c] is [%d]
“, array[i], array[i]); }Prof. RenauShare 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 6.3-compiling RISC-V baremetal 6.4-C vs assembly6.6-C-array in assembly 6.7-C-string in assemblyProf. Renau6.3-compiling RISC-V baremetal 6.4-C vs assemblyNext ClassProf. Renau CS : assignmentchef QQ: 1823890830 Email: [email protected]
Reviews
There are no reviews yet.