Skip to content

Latest commit

 

History

History
220 lines (144 loc) · 10.1 KB

File metadata and controls

220 lines (144 loc) · 10.1 KB

0x02. C - Functions, nested loops

Resources

Tests

  • A Makefile is provided to compile all the tests.
  • Usage: make [test_name]
  • Example: make 1 will compile and run the test for task 1.
  • Example: make check-all will compile and run all tests.
  • Example:make clean will remove all executable files.
  • Some test's output will not be displayed in the terminal only SUCCESS or FAILURE.
  • To print the full output of the particular test, run the executable file directly using ./[test_name].
  • All tests are in the tests directory.
  • All tests are compiled with gcc -Wall -Werror -Wextra -pedantic -std=gnu89 -g3 -o <name_of_test> <name_of_test>.c <name_of_file>.c
  • All tests are run with ./<name_of_test>

Tasks

  • Note for all these challenges we will not be using global variables and any standard library.
  • We will be using function prototypes that will be included in our header file called, main.h.
  • We are allowed to use the prototype funtion _putchar.
  • All *-main.c files will be located in the main directory

0. _putchar

image

  • Below is the assembly code of the program with comments.
<main>: endbr64
<main+4>: push rbp                                  ; \ standard function
<main+5>: mov rbp,rsp                               ; / prologue code
<main+8>: sub rsp,0x20                              ; allocate space for stack array
<main+12>: mov rax,QWORD PTR fs:0x28                ; stack-guard moved to rax
<main+21>: mov QWORD PTR [rbp-0x8],rax              ; stack-guard moved to rbp-0x8
<main+25>: xor eax,eax                              ; zeroing out eax
<main+27>: movabs rax,0x726168637475705f            ; string '_putchar' moved to rax
<main+37>: mov QWORD PTR [rbp-0x11],rax             ; string moved to rbp-0x11
<main+41>: mov BYTE PTR [rbp-0x9],0x0               ; rbp-0x9 = 0
<main+45>: mov DWORD PTR [rbp-0x18],0x0             ; rbp-0x18 (ch) = 0
<main+52>: jmp 0x5555555551e0 <main+78>             ; jump
<main+54>: mov eax,DWORD PTR [rbp-0x18]             ; move rbp-0x18 (ch) to eax
<main+57>: cdqe                                     ; rax = eax (ch)
<main+59>: movzx eax,BYTE PTR [rbp+rax*1-0x11]      ; eax = rbp-0x11[ch]
<main+64>: movsx eax,al                             ; mov 8-bit str chr value to eax
<main+67>: mov edi,eax                              ; mov str chr to edi (arg1)
<main+69>: call 0x555555555169 <_putchar>           ; print character
<main+74>: add DWORD PTR [rbp-0x18],0x1             ; increament rbp-0x18 by 1
<main+78>: cmp DWORD PTR [rbp-0x18],0x8             ; is ch <= 8
<main+82>: jle 0x5555555551c8 <main+54>             ; jump
<main+84>: mov edi,0xa                              ; mov char '\n' to edi (arg1)
<main+89>: call 0x555555555169 <_putchar>           ; print new line char
<main+94>: mov eax,0x0                              ; eax = 0
<main+99>: mov rdx,QWORD PTR [rbp-0x8]              ; mv stack-guard to rdx
<main+103>: sub rdx,QWORD PTR fs:0x28               ; rdx should be 0
<main+112>: je 0x555555555209 <main+119>            ; leave function when equal
<main+114>: call 0x555555555070 <__stack_chk_fail@plt>
<main+119>: leave
<main+120>: ret
  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c 0-putchar.c -o 0-putchar
1. I sometimes suffer from insomnia. And when I can't fall asleep, I play what I call the alphabet game

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/1-main.c 1-alphabet.c -o 1-alphabet
2. 10 x alphabet

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/2-main.c 2-print_alphabet_x10.c -o 2-alphabet_x10
3. islower

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/3-main.c 3-islower.c -o 3-islower
4. isalpha

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/4-main.c 4-isalpha.c -o 4-isalpha
5. Sign

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/5-main.c 5-sign.c -o 5-sign
6. There is no such thing as absolute value in this world. You can only estimate what a thing is worth to you

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/6-main.c 6-abs.c -o 6-abs
7. There are only 3 colors, 10 digits, and 7 notes; it's what we do with them that's important

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/7-main.c 7-print_last_digit.c -o 7-last_digit
8. I'm federal agent Jack Bauer, and today is the longest day of my life

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/8-main.c 8-24_hours.c -o 8-24
9. Learn your times table

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/9-main.c 9-times_table.c -o 9-times_table
10. a+b

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/10-main.c 10-add.c -o 10-add
11. 98 Battery Street, the OG

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/11-main.c 11-print_to_98.c -o 11-98
12. The World looks like a multiplication-table, or a mathematical equation, which, turn it how you will, balances itself

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 _putchar.c main/100-main.c 100-times_table.c -o 100-times_table
13. Nature made the natural numbers; All else is the work of women

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 101-natural.c -o 101-natural
14. In computer class, the first assignment was to write a program to print the first 100 Fibonacci numbers. Instead, I wrote a program that would steal passwords of students. My teacher gave me an A

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 102-fibonacci.c -o 102-fibonacci
15. Even Liber Abbaci

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 103-fibonacci.c -o 103-fibonacci
16. In computer class, the first assignment was to write a program to print the first 100 Fibonacci numbers. Instead, I wrote a program that would steal passwords of students. My teacher gave me an A+

image

  • Compile this way: gcc -Wall -pedantic -Werror -Wextra -std=gnu89 104-fibonacci.c -o 104-fibonacci
--- ## RTFM