added fizzbuzz chapter

This commit is contained in:
Perry Kivolowitz 2022-06-09 20:17:58 -05:00
parent 94220f9837
commit 9c8ca24a6c
4 changed files with 111 additions and 55 deletions

View file

@ -10,6 +10,8 @@
"iostream", "iostream",
"memcpy", "memcpy",
"pseudocode", "pseudocode",
"stringstream",
"strncpy",
"struct", "struct",
"structs" "structs"
], ],

View file

@ -53,6 +53,7 @@ the 64 bit ARM Instruction Set Architecture (ISA).
| 5 | [Interlude - Load and Store](./section_1/regs/ldr.md) | | 5 | [Interlude - Load and Store](./section_1/regs/ldr.md) |
| 6 | [Calling and Returning From Functions](./section_1/funcs/README.md) | | 6 | [Calling and Returning From Functions](./section_1/funcs/README.md) |
| 7 | [Passing Parameters To Functions](./section_1/funcs/README2.md) | | 7 | [Passing Parameters To Functions](./section_1/funcs/README2.md) |
| 8 | [FizzBuzz - a Complete Program](./section_1/fizzbuzz/README.md) |
## Section 2 - Stuff ## Section 2 - Stuff

View file

@ -0,0 +1,51 @@
# Section 1 / Chapter 8 / FizzBuzz
In this chapter we build the classic tech interview question: FizzBuzz.
The idea is simple. Write a program that enumerates the integers from 0 to some stopping value, perhaps 100.
For each integer:
* If it is a multiple of 3, print Fizz
* If it is a multiple of 5, print Buzz
* If it is a multiple of both 3 *and* 5, print FizzBuzz
* Otherwise, if none of the above applies, print the integer.
The interviewer's hope is that you get twisted in knots trying
to navigate the case where the integer is a multiple of both 3 and
5. There are many ways to solve this challenge.
One way might be to test for being a multiple of 15 *first* and print
FizzBuzz if true. Then test against 3 and then against 5.
Another way is to accumulate the correct out by testing against 3 and
adding Fizz to a buffer. Then test against 5 and if appropriate append
Buzz to the buffer. Either the buffer was empty, in which case you get
Buzz alone - or it already contained Fizz in which case the buffer now
contains FizzBuzz. Finally, if *anything* is in the buffer, cause the
buffer to be printed and append a new line.
In C++, the buffer could be a C++ string or a stringstream. In C
you might think that you must resort to using an array of `char` to
act as the buffer, filling it with `strncpy` or some such nonsense.
But you don't have to bother! `printf` is a buffered output stream.
It won't print anything until it encounters a new line character.
In this program, we'll use this to buffer up either Fizz, Buzz or
both then as indicated above, we'll end with a new line and BAM -
whatever was in the `printf` buffer gets sent to the console.
[Here is a video](https://youtu.be/aJSGTIxu4ik) where we walk through
the process of writing FizzBuzz from scratch in ARM 64 bit assembly
language.
[Here is the source code](./fizzbuzz.s).
The video is long but there is much benefit to be had by watching
and listening to another person's process as they write the code.
**AND especially** listening and watching to them debug when
things go wrong!

View file

@ -1,6 +1,3 @@
/* Perry Kivolowitz
CSC3510
*/
.global main .global main
.text .text
.align 2 .align 2
@ -21,9 +18,14 @@
*/ */
/* mod(a, b) - implements a % b /* mod(a, b) - implements a % b
integer divide a by b - for example - 5 % 3 would need 5 // 3 yielding 1 a comes to us in x0
b comes to us in x1
method:
integer divide a by b - for example - 5 % 3
would need 5 // 3 yielding 1
multiply result by b - 1 * 3 is 3 multiply result by b - 1 * 3 is 3
subtract result from a - 5 - 3 yielding 2 and that's our return value. subtract result from a - 5 - 3 yielding 2
and that's our return value.
*/ */
mod: sdiv x2, x0, x1 // x2 gets a // b mod: sdiv x2, x0, x1 // x2 gets a // b
msub x0, x2, x1, x0 // x0 gets a - a // b * b msub x0, x2, x1, x0 // x0 gets a - a // b * b