Skip to content

Instantly share code, notes, and snippets.

@MuntashirAkon
Last active November 4, 2025 04:05
Show Gist options
  • Save MuntashirAkon/2ef30fe5b6d6e437a6c47ae761f8df0e to your computer and use it in GitHub Desktop.
Save MuntashirAkon/2ef30fe5b6d6e437a6c47ae761f8df0e to your computer and use it in GitHub Desktop.
For CS 153 course at UCR
#include "types.h"
#include "user.h"
int test(int n) {
int x = n + 1;
return x;
}
int test2(int n) {
int a[1013] = {0};
printf(1, "Test 2: array a is at %p\n", a);
//printf(1, "Test 2: the first value is %d\n", a[0]);
if(n<2)
return n;
else
return test2(n-1)+1;
}
int main(int argc, char *argv[]) {
int option = atoi(argv[1]);
int num = atoi(argv[2]);
if (option == 1){
int i = argc;
printf(1, "ADDR:_%p_\n", &i);
exit();
}
printf(1, "Test 2: Stack growth test.\n");
printf(1, "test2:_%d_\n", test2(num));
exit();
}
#include "types.h"
#include "user.h"
// Prevent this function from being optimized, which might give it closed form
#pragma GCC push_options
#pragma GCC optimize ("O0")
static int
recurse(int n)
{
if(n == 0)
return 0;
return n + recurse(n - 1);
}
#pragma GCC pop_options
int
main(int argc, char *argv[])
{
int n, m;
if(argc != 2){
printf(1, "Usage: %s levels\n", argv[0]);
exit();
}
n = atoi(argv[1]);
printf(1, "Lab 3: Recursing %d levels\n", n);
m = recurse(n);
printf(1, "Lab 3: Yielded a value of %d\n", m);
exit();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment