Blog

  • new program: chastdin

    I wrote another program which is actually a modification of chastack. This gets input from a user while it is running. Despite how simple it may seem, I had to work at reading from the keyboard because there are multiple ways to read a string from the user. I may add more to this program later, but it has all the important functions of a stack based calculator. Here is a screenshot that shows me using it. You can probably figure out what the commands do based on their name and the numbers printed. I have also attached the full assembly source code to this post.

    main.asm

    format ELF executable
    entry main
    
    include 'chastelib32.asm'
    include 'chastdin32.asm'
    
    main:
    
    mov dword[radix],10    ;I can choose the radix for integer output!
    mov dword[int_width],1 ;and the width of each integer for padded zeros
    
    mov ebp,chastack       ;mov the address of the beginning of the stack to ebp registers
    
    ;this program does not read command line arguments
    ;it always displays a message to tell user what the program does
    mov eax,string_help
    call putstring
    
    mov [last_char],0xA ;set newline as last_char so prompt will display
    
    main_loop:
    
    ;show the arrow indicating we wait for the user to enter something
    ;but only show it when the last character is a newline
    ;otherwise it will print too many if multiple commands were entered on the same line
    cmp [last_char],0xA
    jnz skip_prompt
    mov eax,string_prompt
    call putstring
    skip_prompt:
    
    call getstring ;get string and return address in eax
    
    ;we must restart the loop in case of an empty string
    ;if we didn't, strint would read the empty string and return 0
    ;then zero would be pushed to the stack, which is not what we want
    
    cmp dword[count],0 ;were there zero characters read?
    jz main_loop ;if yes, this was an empty string, retry input
    
    mov esi,eax    ;mov string to esi for string comparison
    
    ;Now we process the string the user entered
    ;First, we will try testing for commands
    ;If any of the predefined strings match the string in esi
    ;We jump to the label for that command
    
    mov edi,string_add
    call strcmp
    jz command_add
    
    mov edi,string_sub
    call strcmp
    jz command_sub
    
    mov edi,string_mul
    call strcmp
    jz command_mul
    
    mov edi,string_div
    call strcmp
    jz command_div
    
    mov edi,string_rem
    call strcmp
    jz command_rem
    
    mov edi,string_query
    call strcmp
    jz command_query
    
    mov edi,string_clear
    call strcmp
    jz command_clear
    
    mov edi,string_exit
    call strcmp
    jz command_exit
    
    ;The default command is to turn the argument into a number and push to stack
    command_num:
    
    mov eax,esi          ;mov the string to eax for processing numbers
    call strint          ;try to get a number from the string pointed to by eax
    cmp [strint_error],0 ;did we have zero errors in the strint function?
    jz num_push          ;if there were no errors, push this to stack
    
    mov eax,string_err
    call putstring
    mov eax,esi
    call putstring
    call putline
    jmp num_push_end ;skip the push because this can't be used
    
    num_push:        ;push the number to the fake stack
    add ebp,4
    mov [ebp],eax
    num_push_end:
    jmp main_loop
    
    ;These are the labels and code for each of the commands
    ;When a command is done, we jump back to the beginning of the loop
    
    command_add:
    mov eax,[ebp]
    mov dword[ebp],0
    sub ebp,4
    add [ebp],eax
    jmp main_loop
    
    command_sub:
    mov eax,[ebp]
    mov dword[ebp],0
    sub ebp,4
    sub [ebp],eax
    jmp main_loop
    
    command_mul:
    mov ebx,[ebp]
    mov dword[ebp],0
    sub ebp,4
    mov eax,[ebp]
    mov edx,0     ;zero edx before multiply
    mul ebx       ;multiply eax with value in ebx
    mov [ebp],eax
    jmp main_loop
    
    command_div:
    mov ebx,[ebp]
    mov dword[ebp],0
    sub ebp,4
    mov eax,[ebp]
    mov edx,0 ;zero edx before divide
    div ebx   ;divide eax with value in ebx
    mov [ebp],eax ;store quotient on stack
    jmp main_loop
    
    command_rem:
    mov ebx,[ebp]
    mov dword[ebp],0
    sub ebp,4
    mov eax,[ebp]
    mov edx,0 ;zero edx before divide
    div ebx   ;divide eax with value in ebx
    mov [ebp],edx ;store remainder on stack
    jmp main_loop
    
    command_query: ;print all numbers on the stack
    push ebp ;save value of ebp
    command_query_loop:
    cmp ebp,chastack ;is ebp equal to the address of stack start?
    jz command_query_end  ;if it is, end the putstack loop
    mov eax,[ebp]
    sub ebp,4
    call putint_and_line
    jmp command_query_loop
    command_query_end:
    pop ebp ;restore ebp to what it was before this command
    jmp main_loop
    
    command_clear: ;erase all numbers on the stack
    command_clear_loop:
    cmp ebp,chastack ;is ebp equal to the address of stack start?
    jz command_clear_end  ;if it is, end the putstack loop
    mov dword[ebp],0
    sub ebp,4
    jmp command_clear_loop
    command_clear_end:
    jmp main_loop
    
    command_exit: ;end the program
    
    main_loop_end:
    
    mov eax,1        ;exit (kernel opcode 1 on 32 bit systems)
    mov ebx,0        ;return 0 status on exit - 'No Errors'
    int 80h          ;system call for 32-bit Linux kernel
    
    argc dd 0
    
    string_err db 'Error: invalid number or command: ',0 ;Generic error message
    string_add db 'add',0
    string_sub db 'sub',0
    string_mul db 'mul',0
    string_div db 'div',0
    string_rem db 'rem',0
    string_exit db 'exit',0
    string_query db '?',0
    string_clear db 'clear',0
    
    string_prompt db '-> ',0
    
    string_help db 'chastdin is a stack based interactive calculator',0xA
                db 'Numbers are pushed on the stack and commands can do math.',0xA
                db 'It is a fork of chastack that reads from stdin instead of arguments.',0xA
                db 'Each line can contain multiple numbers or commands.',0xA
                db 'Math commands are add,sub,mul,div,rem',0xA
                db 'The exit command ends the program',0xA
                db 'The ? command prints the entire stack',0xA,0xA,0
    
    ;This program uses a virtual stack for convenience and portability
    ;I allocate memory for a virtual stack that we can index as if it was the real stack
    ;I name it "chastack" for Chastity's stack.
    
    db 6 dup 0 ;extra padding bytes
    chastack: rd 0x100
    

    chastdin32.asm

    ;Chastity's Standard Input header file
    ;The functions here are designed to read strings and numbers from standard input.
    
    ;getstring ;read characters from stdin until the first whitespace
    ;getline   ;read characters from stdin until the first newline,EOF,tab,etc.
    ;strcmp    ;compare two strings similar to the same function in C
    
    ;these variables are used as the default controllers
    ;for the getstring and getline functions
    ;buf stores keyboard input during those functions
    ;count stores how many bytes were read
    ;last_char stores the last character read
    ;usually this will be a space, tab, or newline
    
    buf db 0x100 dup '?'
    count dd 0
    last_char db 0
    
    ;summary
    ;the getstring function is the reverse function of putstring
    ;instead of printing a string to standard output
    ;it reads a string from standard input (AKA the keyboard)
    
    ;details
    ;the getstring function is designed to get a string of text
    ;which is terminated by whitespace or any non printable character
    ;the idea is that multiple strings can be passed on one line
    ;separated by spaces, similar to command line arguments
    ;this function was written for the specific purpose of converting any of
    ;my programs that used command line arguments to read from stdin instead
    
    getstring:
    
    mov [count],0 ;set count of characters read during this function to zero
    mov edx,1     ;number of bytes to read
    mov ecx,buf   ;address to store the bytes
    
    getstring_chars:
    
    mov ebx,0     ;read from stdin
    mov eax,3     ;invoke SYS_READ (kernel opcode 3)
    int 80h       ;call the kernel
    
    cmp eax,1     ;was 1 character read?
    jnz getstring_end ; if not, then end this loop
    
    mov al,[ecx]  ;mov last character read into al register
    
    ;check if this character is in the proper range to be part of the string
    
    cmp al,0x21      ;compare with 0x21 (!=exclamation)
    jb getstring_end ;jump if below to getstring_end label
    cmp al,0x7E      ;compare with 0x7E (tilde)
    ja getstring_end ;jump if above to getstring_end label
    
    ;if neither jump happened, keep the character and
    
    inc [count]   ;increment how many characters we have read
    inc ecx       ;increment address where next byte is read from
    jmp getstring_chars ;jump back to start of loop and keep reading
    
    getstring_end:
    
    mov [last_char],al ;save the last character read
    mov byte[ecx],0 ;terminate this string with a zero
    
    mov eax,buf ;mov the buffer address to eax for returning the string
    
    ret
    
    ;the getline function gets an entire line of text from the keyboard
    ;calling this function allows for a string that can contain spaces
    ;it considers as anything outside the range of 0x20 to 0x7E as the end of line character
    ;this is because the end of the line might be 0x0A on Linux
    ;or it might be 0x0D,0x0A on DOS or Windows.
    ;technically, it means tab will also terminate a line
    ;the intended use of this function is to read a filename
    ;filenames can contain spaces
    
    getline:
    
    mov [count],0 ;set count of characters read during this function to zero
    mov edx,1     ;number of bytes to read
    mov ecx,buf   ;address to store the bytes
    
    getline_chars:
    
    mov ebx,0     ;read from stdin
    mov eax,3     ;invoke SYS_READ (kernel opcode 3)
    int 80h       ;call the kernel
    
    cmp eax,1     ;was 1 character read?
    jnz getline_end ; if not, then end this loop
    
    mov al,[ecx]  ;mov last character read into al register
    
    ;check if this character is in the proper range to be part of the string
    
    cmp al,0x20    ;compare with 0x20 (space)
    jb getline_end ;jump if below to getstring_end label
    cmp al,0x7E    ;compare with 0x7E (tilde)
    ja getline_end ;jump if above to getstring_end label
    
    ;if neither jump happened, keep the character and
    
    inc [count]       ;increment how many characters we have read
    inc ecx           ;increment address where next byte is read from
    jmp getline_chars ;jump back to start of loop and keep reading
    
    getline_end:
    
    mov byte[ecx],0 ;terminate this string with a zero
    
    mov eax,buf ;mov the buffer address to eax for returning the string
    
    ret
    
    ;summary
    ;strcmp compares the string at esi to the one at edi
    ;eax returns 0 if the strings are the same and 1 if different
    ;the algorithm is simple but I will explain it for those who are confused
    
    ;details
    ;eax is initialized to zero
    ;a byte from each string is loaded into the al and bl registers
    ;the bytes are compared. if they are different, then we jump to the end
    ;However, if they are the same, then we check if one of them is zero
    ;for this purpose it doesn't matter whether we compare al or bl with zero
    ;because it is known that they are the same if the jnz did not take place
    ;if it is zero, this also jumps to the end of the function
    ;If neither jump took place, then we jump to the start of the loop
    ;but when the function finally ends bl will be subtracted from al
    ;this ensures that the function returns zero if the final characters are the same
    ;ebx,esi,and edi are preserved but eax is the return value
    ;also, the sub instruction at the end of the function also updates the flags
    ;so you can "jz" or "jnz" to a label after calling this function based on results
    
    strcmp:
    
    push ebx
    push esi
    push edi
    
    mov eax,0
    
    strcmp_start:
    
    ;read a byte from each string
    mov al,[edi]
    mov bl,[esi]
    cmp al,bl
    jnz strcmp_end
    
    cmp al,0
    jz strcmp_end
    
    inc edi
    inc esi
    
    jmp strcmp_start
    
    strcmp_end:
    sub al,bl
    
    pop edi
    pop esi
    pop ebx
    
    ret
    
  • AAA Linux: Chapter 15: chastecmp

    This post is a chapter from my recently published Linux edition of Assembly Arithmetic Algorithms. The story behind why I wrote the program featured in chapter 15 goes back to when I discovered how to cheat at video games. This story is worth sharing to inspire the next generation of gamers to learn computer math and programming just as I did.

    Chapter 15: chastecmp

    In this chapter, I will show you the source code of a file comparison program. This program is meant to find which bytes are different between two files that are similar but contain a few differences.

    I will use text files for my examples in this chapter, but the program actually does a binary file comparison and displays the different bytes in hexadecimal because it is a universally understood shorthand for binary that most C and Assembly programmers are already familiar with.

    First, here is the source code of chastecmp, which is the short name for “Chastity’s Comparison tool”. The name is also meant to refer to the “cmp” instruction, which is used a lot more in this program because it is essential.

    FASM chastecmp source

    ;Linux 32-bit Assembly Source for chastecmp
    format ELF executable
    
    main:
    
    ;radix will be 16 because this whole program is about hexadecimal
    mov dword[radix],16 ; can choose radix for integer input/output!
    mov dword[int_width],1
    
    pop eax ;get the number of arguments
    dec eax ;subtract 1 because we will ignore the name of the program
    pop ebx ;pop program name into a register to delete it from stack
    
    cmp eax,2 ;do we have two arguments to be used as filenames?
    jb help
    mov dword[offset],0 ;assume the offset is 0,beginning of file
    jmp arg_open_file_1
    
    help:
    mov eax,help_message
    call putstring
    jmp main_end
    
    arg_open_file_1:
    pop eax
    mov [filename1],eax ; save the name of the file we will open to read
    call putstring ;print the name of the file we will try opening
    
    mov ecx,0   ;open file in read mode 
    mov ebx,eax ;move filename for system call
    mov eax,5   ;invoke SYS_OPEN (kernel opcode 5)
    int 80h     ;call the kernel
    
    cmp eax,0
    js file_error_display ;end program if the file can't be opened
    mov [fd1],eax ; save the file descriptor number for later use
    mov eax,file_open
    call putstr_and_line
    
    arg_open_file_2:
    pop eax
    mov [filename2],eax ; save the name of the file we will open to read
    
    call putstring ;print the name of the file we will try opening
    
    mov ecx,0   ;open file in read mode 
    mov ebx,eax ;move filename for system call
    mov eax,5   ;invoke SYS_OPEN (kernel opcode 5)
    int 80h     ;call the kernel
    
    cmp eax,0
    js file_error_display ;end program if the file can't be opened
    mov [fd2],eax ; save the file descriptor number for later use
    mov eax,file_open
    call putstr_and_line
    
    files_compare:
    
    file_1_read_one_byte:
    mov edx,1       ;number of bytes to read
    mov ecx,buf1    ;address to store the bytes
    mov ebx,[fd1]   ;move the opened file descriptor into EBX
    mov eax,3       ;invoke SYS_READ (kernel opcode 3)
    int 80h         ;call the kernel
    
    ;eax will have the number of byte read after system call
    mov [count1],eax ;we save the number of byte read for later
    cmp eax,0
    jnz file_2_read_one_byte ;unless zero bytes were read, proceed to read from next file
    
    mov eax,[filename1]
    call putstring
    mov eax,end_of_file_string
    call putstr_and_line
    
    ;Even if we have reached the end of the first file,
    ;we still proceed to read a byte from the second file
    ;to see if it also ends at the same address
    
    file_2_read_one_byte:
    mov edx,1       ;number of byte to read
    mov ecx,buf2    ;address to store the bytes
    mov ebx,[fd2]   ;move the opened file descriptor into EBX
    mov eax,3       ;invoke SYS_READ (kernel opcode 3)
    int 80h         ;call the kernel
    
    ;eax will have the number of bytes read after system call
    mov [count2],eax ;we save the number of bytes read for later
    cmp eax,0
    jnz check_both_bytes ;unless zero bytes were read, proceed to compare bytes from both files
    
    mov eax,[filename2]
    call putstring
    mov eax,end_of_file_string
    call putstr_and_line
    
    jmp main_end ;we have reach end of one file and should end program
    
    check_both_bytes:
    
    ;we add the number of bytes read from both files
    mov eax,[count1]
    add eax,[count2]
    cmp eax,2
    jnz main_end
    
    compare_bytes:
    
    mov al,[buf1]
    mov bl,[buf2]
    
    ;compare the two bytes and skip printing them if they are the same
    cmp al,bl
    jz bytes_are_same
    
    ;print the address and the bytes at that address
    mov eax,[offset]
    mov dword[int_width],8
    call putint_and_space
    mov dword[int_width],2
    mov eax,0
    mov al,[buf1]
    call putint_and_space
    mov al,[buf2]
    call putint_and_line
    
    bytes_are_same:
    
    inc dword[offset]
    
    jmp files_compare
    
    file_error_display:
    
    mov eax,file_error
    call putstr_and_line
    
    main_end:
    
    ;this is the end of the program
    ;we close the open files and then use the exit call
    
    mov ebx,[fd1] ;file number to close
    mov eax,6   ;invoke SYS_CLOSE (kernel opcode 6)
    int 80h     ;call the kernel
    
    mov ebx,[fd2] ;file number to close
    mov eax,6   ;invoke SYS_CLOSE (kernel opcode 6)
    int 80h     ;call the kernel
    
    mov eax, 1  ; invoke SYS_EXIT (kernel opcode 1)
    mov ebx, 0  ; return 0 status on exit - 'No Errors'
    int 80h
    
    include 'chastelib32.asm'
    
    ;variables for displaying information
    help_message db 'chastecmp by Chastity White Rose',0Ah,0Ah
    db 9,'chastecmp file1 file2',0Ah,0Ah
    db 'Differing bytes are shown in hexadecimal',0Ah
    db 'until the EOF has been reached.',0Ah,0
    
    file_open db ' opened',0
    file_error db ' error',0
    end_of_file_string db ' EOF',0
    
    db 23 dup 0 ;fill with extra space to match 1024 executable size
    
    ;variables for managing files
    filename1 dd ? ;name of the file to be opened
    filename2 dd ? ;name of the file to be opened
    fd1 dd ?       ;file descriptor 1
    fd2 dd ?       ;file descriptor 2
    buf1 db ?      ;store byte from file 1 here
    buf2 db ?      ;store byte from file 2 here
    count1 dd ?    
    count2 dd ?
    offset dd ?
    

    How to use chastecmp

    Using the chastecmp program requires two filenames to be passed as command-line arguments. Although you can use any files you have, it makes sense to use a simple example with text files because they are so easy to create with the echo command.

    Run these commands to create the two files.

    echo "chandler is my birth name" > file1.txt
    echo "chastity is my trans name" > file2.txt
    

    Now that the files exist

    ./main file1.txt file2.txt
    

    If you have created these files and run the chastecmp program on them, you will see this result:

    file1.txt opened
    file2.txt opened
    00000003 6E 73
    00000004 64 74
    00000005 6C 69
    00000006 65 74
    00000007 72 79
    0000000F 62 74
    00000010 69 72
    00000011 72 61
    00000012 74 6E
    00000013 68 73
    file1.txt EOF
    file2.txt EOF
    

    How does chastecmp work?

    This program is much simpler than chastack or chastext, but it is close to 180 lines and still has some logic to follow. First thing it does is check to see how many command-line arguments were passed to the program. Since the name of the program always counts as 1, we subtract from this number and also pop the next argument into ebx just to get rid of it. The actual register used doesn’t matter in this case as long as it is not eax, which holds the number of arguments.

    The eax register is compared with 2. If this number is below 2, then there are not enough arguments to continue the program, and it will end. Otherwise, it will proceed to use the open call with both filenames and assume these files exist. If they do not exist, it will print the filename and then say error.

    If both files are opened, it will keep reading 1 byte from each file descriptor and store each in its own buffer of 1 byte. If the two bytes are the same, they will be ignored. However, if they are different, the address and the values of both bytes at that address will be displayed.

    The variable “offset” is used to keep track of which address we are at in both files, but it isn’t used to lseek in this program because we are going from beginning to end.

    If at any time the read system call returns 0, a message is displayed with the filename and EOF to tell the user that the end of that file has been reached.

    In the example I just used, both files are the same length of 26 bytes and will reach the end at the same time.

    But why should I care?

    The average person probably does not know why it matters to see the hexadecimal differences between two files. I know it seems silly, especially for small text files as I used in this chapter’s examples. However, I can give two examples of times I have used this information.

    The first example is relevant to Chapter 2, where I presented the header file “chaste-elf-32.nasm” which can be included to make a loadable program using the NASM assembler.

    I read the specification document for ELF files to describe what the fields were named and what the values meant. However, this informational alone was not enough for me to successfully create the custom ELF header. I had to create ELF executable files with FASM because it has this feature built in. By creating slightly different programs, I was able to compare the binary differences in the different source files fed to FASM. The chastecmp program was extremely helpful to me as I used it hundreds of times in reverse engineering the ELF format.

    One of my discoveries was that when the size of a program increased, either by adding more code or adding more data statements, there was a number in the header that also increased. As it turns out, the memory size of the file increased even when data reservation keywords (such as rb,rw,rd, and rq) were used, even when the size of the file itself didn’t.

    The specification could tell me a lot, but without the example ELF headers FASM was already creating, I would not have been able to create dynamic headers to match programs written in FASM. I probably spent 12 hours on that project, but at least I can assembler any of my programs with NASM if I make the necessary syntax changes.

    But perhaps a more fun example, and also the reason I got started with programming, was that I used a file comparison tool to cheat at a Norse mythology game years ago. The game was called Castle of the Winds, and it ran on Windows 3.1, 98, and even XP.

    One of the features of that specific game was that it let you save the game at any time. I remember that I had 5 mana points. I saved the first file and then cast the magic arrow spell to spend one point. I then saved a second file and ran the Windows “fc” command to compare the two files in binary mode.

    fc /b 1.cwg 2.cwg
    

    It told me the address of the byte that had changed from 5 to 4. I then opened this in a hex editor named XVI32 and changed this byte to different values.

    In time, I was able to not only change my mana points but also hit points and experience points to make myself invincible in that game.

    I didn’t really know much about hexadecimal at this point, but by trial and error, I accidentally started understanding it. It was this experience of cheating in a video game that led me to learn about binary and hexadecimal number systems originally.

    I had seen for the first time that an understanding of computer arithmetic could allow me to break the rules and do things in a video game that the developer could not predict or prevent me from doing. In those days, I learned to do the same with many video games and had many fun adventures.

    In modern times, developers have gotten smarter and have put measures in place to prevent this form of cheating. Most notably, more games are multiplayer and read data from a server that stores the game data, where no user can hack it.

    But you have to understand that back in the 90s, nearly every single player game could be hacked that stored its data locally and didn’t connect to the internet. I have had people criticize my habit of cheating in single-player games and say that it ruins the experience of the game.

    But what they don’t understand is that I didn’t care about the video game I was hacking, because Arithmetic had become my favorite game. My love of math was so great that I learned computer programming and had more fun writing programs in BASIC, C, and Assembly than I did playing video games in the first place.

    I can’t hack most modern games with these tricks, but I have found the art of computer programming, which is much more satisfying than any video game I have played in my life.

    In summary, the chastecmp program does the same thing as the “fc /b” command from DOS and Windows did. When I switched to Linux as my primary operating system, I wrote my own file comparison tool to always keep the fond memories of my childhood with me.

  • ELF Header Assembly Gists on Github

    I had just finished the 14th chapter of the Linux edition of Assembly Arithmetic Algorithms, and then I took a look at some old source code where I had learned enough of the ELF format to create my own ELF headers using either FASM or NASM. The differences between these two assemblers make them incompatible with each other. Still, I have made four separate gists on GitHub so that if someone wants to create their own ELF header for a program without relying on the built-in features of FASM, I have found the best way to do it.

    The purposes of this are both for my own greater understanding of the ELF format, because it is used in all Linux distributions, and also to provide a way for NASM users to benefit from features that currently only exist in FASM, which is still my favorite Assembler.

    FASM Hello 32-bit

    FASM Hello 64-bit

    NASM Hello 32-bit

    NASM Hello 64-bit

    Although my Linux Assembly book mostly focuses on using FASM because it is easier for beginners, I do officially support those who wish to port my programs to other assemblers. Porting is easy once you get past the small differences in the directives of each.

  • putchar

    putchar

    int putchar(int c);

    What does this function do?

    Writes 1 character to standard output (terminal or redirected to file).

    When would you use this function?

    1. When you need to write a simple character for formatting, such as a space, tab, newline, etc. to separate the output of other output functions like printf or fwrite.
    2. To print a specific range of characters in a loop.

    Where in your program would you call it?

    Any place where you need a character to separate data or you are unsure of the value of a byte and need to debug something. This is especially true in loops to identify where bugs are.

    Why is this function better or worse than alternatives?

    putchar is simpler to use than printf and only requires one argument: the character number.

    How can you use it?

    Here is a complete program using putchar that writes all the printable characters to the terminal using a loop. It then prints a new line character with the escape sequence '\n'.

    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
     int a=0x20,b=0x7F;
    
     while(a<b)
     {
      putchar(a);
      a+=1;
     }
     putchar('\n');
        
     return 0;
    }
    

    Compile and run

    gcc -Wall -ansi -pedantic main.c -o main && ./main

    Output

     !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
    
  • Chapter 9: Chad and Luke Share Bible Stories

    Chad was hard at work finding Bible stories he could tell the kids while they ate after soccer. He had managed to speak the story from Daniel chapter 1 in his own words, which made more sense than how it sounded in the King James Bible.

    Meanwhile, Luke was also busy picking his own set of Bible stories. He was looking for something he could use to fight against Chad’s Vegan diet and also justify his own beliefs about eating meat. He also remembered that Chad said he could bring any food he wanted for the children. He decided to buy some regular hot dogs and buns because he knew it would make Chad angry.

    On the next Sunday, Chad and Luke coached the kids in soccer, and everyone had a lot of fun. There seemed to be more children there than the previous week. Chad said it was probably because they told their friends about the fun of soccer and free food.

    When it was time for lunch, Chad got a big cooler out of his car and bags of chips. This choice of food surprised Luke, because he had expected Tofu sandwiches. He also noticed that there was no grill, and Stacy wasn’t there.

    Chad served everyone a plate full of chips and filled several cups with cold lemonade. He brought a cup to each child and told them to come to the table for more if they were still thirsty.

    “Chad, do you have a Bible story prepared today?” said Luke.

    “You bet I do, but first I need to drink some lemonade,” said Chad.

    “In that case, I will tell my story,” said Luke.

    “The story begins in Genesis chapter 6, when humans were so evil that God regretted making them. He decided the earth needed a great reset. He liked a man named Noah, who was righteous, and told him to build a big boat called an ark. He and his family would collect animals and lots of food to eat during a flood that would drown every other living thing. The only humans on the ark were Noah, his wife, their 3 sons, and their wives. They also had a male and female of every kind of animal,” said Luke.

    “Why did all the animals on the earth have to die?” said Simon.

    “They were not necessary because the animals on the ark would reproduce after the flood, and there would be more of all the animals,” said Luke.

    “What does it mean to reproduce?” asked Simon.

    "You might want to ask your parents about that. I am not qualified to answer that, especially for children. Anyhow, that’s not the point. After the flood ended, God gave humans permission to eat animals for food. In Genesis chapter 9, God said

    
    And God blessed Noah and his sons, and said unto them, Be fruitful, and multiply, and replenish the earth.
    
    2 And the fear of you and the dread of you shall be upon every beast of the earth, and upon every fowl of the air, upon all that moveth [upon] the earth, and upon all the fishes of the sea; into your hand are they delivered.
    
    3 Every moving thing that liveth shall be meat for you; even as the green herb have I given you all things.
    
    

    “So you see, animals exist to be eaten, which reminds me, I brought hot dogs,” said Luke.

    The children hissed and booed at Luke. They wanted no part of eating animals, and they were quite traumatized by the Noah’s Ark story. They asked Chad to tell another Daniel story.

    “Of course, I can tell you a story,” said Chad.

    “Many years after King Nebuchadnezzar died, and also his son Belshazzar was killed, a new king named Darius took over. In spite of this, Daniel was still trusted by the new king and still considered a wise man or a prophet because he had interpreted dreams for King Nebuchadnezzar and had translated the writing on the wall for Belshazzar. He was assigned to be in authority over the princes and presidents,” said Chad.

    “The presidents and princes became jealous of Daniel because he was their boss and the king liked him more. Therefore, they tried to find a way to turn the king against him. They asked King Darius to write a decree that for the next 30 days, anyone who prays to any God or human other than him would be thrown into a den of lions. He agreed and wrote a decree that anyone caught praying to someone else would be executed by being thrown into the Lion’s Den. Though he didn’t realize that this was a trap being set for his most trusted servant, Daniel,” said Chad.

    “The men who asked him to make the decree knew they would find Daniel praying near his window as he always had. Daniel knew about the law, but he still prayed to God 3 times a day and didn’t change anything he did.”

    “Why didn’t he just keep his prayers hidden or hide somewhere where the bad guys couldn’t catch him?” said a young boy named Trevor.

    “I think that he knew it was better to be faithful to God and live in his truth, even though he knew he might be in trouble for it,” said Chad.

    “What happened after that?” said multiple children.

    “The princes and presidents told the king that Daniel was still praying to his God, despite the decree. They demanded that he be thrown into the Lion’s Den as was written in the executive order,” said Chad.

    “The king tried to talk his way out of his, but the law of the king could not be disobeyed even by the king himself because he would be admitting he made a mistake and was not a God. He knew he had been tricked by the bad guys. Daniel was thrown into the Lion’s Den, and the king could not eat or sleep that night because he was so worried about Daniel. The next day, he went and found that Daniel was still alive. The lions had not even touched him. Daniel said that God closed the mouth of the lions because he was innocent,” said Chad.

    “At that moment, the king ordered Daniel to be taken out of the den and to throw in all the men who had accused Daniel. The lions tore them each up before they even hit the ground. He then made a new executive order that everyone should worship and pray to the God of Daniel. He believed that he was the true God who could make sure that even hungry lions would not eat the innocent,” said Chad.

    “Come on, Chad, do you really believe this story? Those lions would have eaten Daniel if he were in there all night,” said Luke.

    “Tell me, Luke, do you believe the flood story you told the children in Genesis? Do you really think that a God who prevents lions from eating innocent man would drown all the innocent animals of the whole earth, and that only 8 humans were righteous enough to be saved?” said Chad.

    “Well, these stories are both part of the Bible, so I guess they are both true,” said Luke.

    “But that’s not what I asked you, Luke. I asked whether YOU believe the story you read to the children,” said Chad.

    “I am not sure. But do you believe the story of Daniel in the Lion’s Den?” said Luke.

    “You bet I do. It only makes sense. Daniel was innocent, and he had done nothing wrong. We also know that he did not eat meat from last week’s story. He did not have the smell of meat on him, and the lions probably didn’t think he smelled like food. They probably would have left him alone even if God had not intervened. This story not only matches what I believe, but it makes sense to my brain as well,” said Chad.

    The children clapped and continued to enjoy the salty chips and the cool. Lemonade. Chad knew just what their bodies needed in the summer heat and told a story that they could understand and relate to.

    Nobody touched Luke’s hot dogs, not even him. He was too angry to eat. He was jealous of Chad, just like the princes and presidents who had tried to turn King Darius against Daniel.