Skip to content

Instantly share code, notes, and snippets.

@Gumball12
Last active September 22, 2019 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gumball12/2abbaed7e559605eb4c08a7b752d5200 to your computer and use it in GitHub Desktop.
Save Gumball12/2abbaed7e559605eb4c08a7b752d5200 to your computer and use it in GitHub Desktop.
CSAPP assignments
// import modules
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
// define Stack struct
struct Stack {
int* arr;
int top;
unsigned length;
};
// create stack function
struct Stack* createStack(unsigned len) {
// create stack
struct Stack* stack = (struct Stack*) malloc(sizeof(struct Stack));
// insert values
stack->length = len;
stack->top = -1;
stack->arr = (int*)malloc(stack->length * sizeof(int));
// return stack
return stack;
}
// check stack is full
int isFull(struct Stack* stack) {
// check full
return stack->top == stack->length - 1;
}
// check stack is empty
int isEmpty(struct Stack* stack) {
// check empty
return stack->top == -1;
}
// push item into the stack
int push(struct Stack* stack, int i) {
// check stack is full
if (isFull(stack)) {
return 0;
}
// insert item
stack->arr[++stack->top] = i;
// return top index
return stack->top;
}
// pop from stack
int pop(struct Stack* stack) {
// check stack is empty
if (isEmpty(stack)) {
return 0;
}
// return top value
return stack->arr[stack->top--];
}
// main
int main(void) {
// create a stack
struct Stack* stack = createStack(100);
// push items
push(stack, 10);
push(stack, 20);
push(stack, 30);
// print length
printf("length: %d\n", stack->length);
// pop item
printf("pop: %d\n", pop(stack));
return 0;
}
.file "assignment.c"
.text
.globl createStack
.type createStack, @function
createStack:
.LFB41:
.cfi_startproc
pushq %rbp
.cfi_def_cfa_offset 16
.cfi_offset 6, -16
pushq %rbx
.cfi_def_cfa_offset 24
.cfi_offset 3, -24
subq $8, %rsp
.cfi_def_cfa_offset 32
movl %edi, %ebx
movl $16, %edi
call malloc@PLT
movq %rax, %rbp
movl %ebx, 12(%rax)
movl $-1, 8(%rax)
movl %ebx, %edi
salq $2, %rdi
call malloc@PLT
movq %rax, 0(%rbp)
movq %rbp, %rax
addq $8, %rsp
.cfi_def_cfa_offset 24
popq %rbx
.cfi_def_cfa_offset 16
popq %rbp
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE41:
.size createStack, .-createStack
.globl isFull
.type isFull, @function
isFull:
.LFB42:
.cfi_startproc
movl 12(%rdi), %eax
subl $1, %eax
cmpl 8(%rdi), %eax
sete %al
movzbl %al, %eax
ret
.cfi_endproc
.LFE42:
.size isFull, .-isFull
.globl isEmpty
.type isEmpty, @function
isEmpty:
.LFB43:
.cfi_startproc
cmpl $-1, 8(%rdi)
sete %al
movzbl %al, %eax
ret
.cfi_endproc
.LFE43:
.size isEmpty, .-isEmpty
.globl push
.type push, @function
push:
.LFB44:
.cfi_startproc
movl 8(%rdi), %edx
movl 12(%rdi), %eax
leal -1(%rax), %ecx
movl $0, %eax
cmpl %ecx, %edx
je .L5
movq (%rdi), %rax
addl $1, %edx
movl %edx, 8(%rdi)
movslq %edx, %rdx
movl %esi, (%rax,%rdx,4)
movl 8(%rdi), %eax
.L5:
rep ret
.cfi_endproc
.LFE44:
.size push, .-push
.globl pop
.type pop, @function
pop:
.LFB45:
.cfi_startproc
movl 8(%rdi), %edx
movl $0, %eax
cmpl $-1, %edx
je .L8
movq (%rdi), %rax
leal -1(%rdx), %ecx
movl %ecx, 8(%rdi)
movslq %edx, %rdx
movl (%rax,%rdx,4), %eax
.L8:
rep ret
.cfi_endproc
.LFE45:
.size pop, .-pop
.section .rodata.str1.1,"aMS",@progbits,1
.LC0:
.string "length: %d\n"
.LC1:
.string "pop: %d\n"
.text
.globl main
.type main, @function
main:
.LFB46:
.cfi_startproc
pushq %rbx
.cfi_def_cfa_offset 16
.cfi_offset 3, -16
movl $100, %edi
call createStack
movq %rax, %rbx
movl $10, %esi
movq %rax, %rdi
call push
movl $20, %esi
movq %rbx, %rdi
call push
movl $30, %esi
movq %rbx, %rdi
call push
movl 12(%rbx), %edx
leaq .LC0(%rip), %rsi
movl $1, %edi
movl $0, %eax
call __printf_chk@PLT
movq %rbx, %rdi
call pop
movl %eax, %edx
leaq .LC1(%rip), %rsi
movl $1, %edi
movl $0, %eax
call __printf_chk@PLT
movl $0, %eax
popq %rbx
.cfi_def_cfa_offset 8
ret
.cfi_endproc
.LFE46:
.size main, .-main
.ident "GCC: (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0"
.section .note.GNU-stack,"",@progbits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment