CSE12 C fun
Annoucements
• lab4 soon (Friday, not Sunday last day of class) •Must install WSL (Windows) or brew (OSX)
Copyright By PowCoder代写 加微信 powcoder
•Check video on google drive (riscv-video.mp4) to install
CMPE 110 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
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
•6.8-C-function call in assembly •6.9-C-recursive call in assembly (stack)
Prof. Renau
• variables
• constants
• expressions
• comments
• function
• definition/call
• basic statements • if/while/for
• Variable scope
Prof. Renau
#include
int fun1(int a) {
int b = a;
int a = 3;
printf(“1. a=%d\n”,a);
printf(“2. a=%d\n”,a);
int main() {
int c = fun1(a);
printf(“3. a=%d c=%d\n”,a,c);
Variable Scope
Prof. Renau
#include
int my_global = 3;
void my_fun() {
printf(“g=%d\n”,my_global); }
int main() {
int my_local=4;
printf(“l=%d g=%d\n”,my_local, my_global); my_global = 33;
Local vs Global variables
Prof. Renau
• int array_of_ints[100];
• char array_of_chars[13];
• char *array_of_chars2 = “Hello”; // 6 bytes 5 + zero end
CMPE 110 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\n”,array); printf(“my string is %s\n”,ptr);
for(int i=0;i<8;i++) {
printf("[%c] is [%d]\n", array[i], array[i]); }
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
• We have seen function calls
•how are the local variables managed?
Functions and Stacks
CMPE 110 Prof. Renau
Implementing Function Calls
•A way to pass/return values to/from functions
•A mix of registers and stack •A way to store local variables
Calling function
copy values into arguments call function
get result from stack
Called function
execute code put result in ?? return
Prof. Renau
Stack Operation
Before call
During call
After call
Prof. Renau
Where Does the Stack Reside?
heap or BSS data
Prof. Renau
Simplistic Function Call
int NoName(int a, int b)
int w, x, y;
.return y; }
return value
return address
dynamic link
bookkeeping
int int int int int
NoName NoName NoName NoName NoName
Prof. Renau
More Realistic Function Call
• Do not keep everything in the stack
•Pass some parameters through registers (a0,a1,a2...) •Keep the return address in a register (jal ra, ....)
int fun(int a,int b, char c) {
return a + b + c;
int caller(int a) {
a = fun(a<<2,11,22);
return a + 300;
fun: # @fun(int, int, char)
add a0, a0, a1
add a0, a0, a2
caller: # @caller(int)
addi sp, sp, -16
sw ra, 12(sp)
slli a0, a0, 2
addi a1, zero, 11
addi a2, zero, 22
call fun(int, int, char)
addi a0, a0, 300
lw ra, 12(sp)
addi sp, sp, 16
Prof. Renau
lw ra, 12(sp)
addi sp, sp, 16
caller2: # @caller2(int)
addi sp, sp, -16
# 4-byte Folded Reload
More Realistic Function Call
caller: # @caller(int)
addi sp, sp, -16
int caller(int a) {
b = fun(a<<2,11,22);
return a + b;
int caller2(int a) {
b = fun(a<<2,11,22);
return a + b; }
# 4-byte Folded Spill
sw ra, 12(sp)
slli a0, a0, 2
addi a1, zero, 11
addi a2, zero, 22
call fun(int, int, char)
addi a0, a0, 300
sw ra, 12(sp)
sw s0, 8(sp)
mv s0, a0
slli a0, a0, 2
addi a1, zero, 11
addi a2, zero, 22
call fun(int, int, char)
add a0, a0, s0
lw s0, 8(sp) # 4-byte Folded Reload
lw ra, 12(sp) # 4-byte Folded Reload
addi sp, sp, 16
ret Prof. Renau
# 4-byte Folded Spill
# 4-byte Folded Spill
Function and Scope
int foo(int x, int y) { int z = x + (y++); return z;
int main(void) {
int a = 6;
int b = foo(33, b);
Return address
Prof. Renau
int foo(int x, int y) { int z = x + (y++); return z;
int main(void) {
int a = 6;
int b = foo(33, b);
Function and Scope
Return address
Return address
Prof. Renau
int factorial(int n) {
return n*factorial(n-1);
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
• Finding problems in a problem and fix them
• Scientific process. Create a hypothesis, check it
• Tools for C • debuggers
• Methodologies
•Test Driven Development •Continuous Integration (CI)
Prof. Renau
• Linux/WSL: gdb/lldb
• OSX: lldb
• online: https://learn.onlinegdb.com/
• Commands: •b
•step vs next
Prof. Renau
Test Driven Development (or Behavioral Driven Development)
• Test driven development (TDD/BDD)
•Starts writing test, then goes to write code to make test pass
• Simple rules!
•It does not work unless you test it •Testing must be automated (CI) •Use assertions
• Some free source
• http://jodypaul.com/SWE/TD/TestDebug.html
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
•6.8-C-function call in assembly •6.9-C-recursive call in assembly (stack)
CMPE 110 Prof. Renau
•7-Datapath (2 days)
•7.1-Mem + PC + ALU (COD 4.3)
•7.2-ALU control (COD 4.4)
•7.3-datapath showcase RISC-V instructions
Next Class
Prof. Renau
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com