The following guidelines are expected for all homework submissions:

Problems for Assignment #1

Learning Outcomes: 1) Verifying the development environment is set up properly for GitHub; 2) Verifying the development environment is set up and is functional for compiling C programs; 3) Verifying the development environment is set up and is functional for assembling nasm programs; 4) Verifying that the instructor can access the GitHub repository to provide feedback evaluations during the semester

This homework assignment simply extends the in-class assignment for week one. In classwork01 you set up and compiled a simple C program to make sure it worked. This proved the gcc compiler and linker is working on your computer. It also makes sure your editor is working properly so that you can create source code with it. These are basic steps, but I want to make sure everyone is on equal footing from the start of the semester.

The second part of this homework is to verify that your nasm assembler is working properly, which is also part of the classwork01 exercise.

Part One
  1. Add to your sayhello.c program from your classwork01 in class exercise so that it prompts the user for their name from the terminal command line, using the printf() and the fgets() or scanf() functions. These are also demonstrated in the F to C conversion code on the week01 page. Don't forget you will need to include BOTH the stdio.h and stdlib.h header files in your source file, just like you need to do in Java or Python using the import keyword:
       #include <stdio.h>
       #include <stdlib.h>
    
       // main method and code go here...
                        
  2. Add more lines to your program to change the Hello, World! message to say Hello, followed by the name that the user has typed in
  3. Compile and run your program to make sure everything works
  4. Test your program with several different inputs, like dog, $237.19, and just pressing enter without entering anything. See if you can break your program and make it crash!
  5. Go to GitHub and set up your repo and submission structure if you haven't already done so; make it look like the classwork and homework structure that is on the instructor's repo at this link.
  6. Upload and commit your completed sayhello.c file to your repo under the homework/homework01 folder.
  7. For a little extra challenge, see if you can dress up your program a bit by making it output some extra blank lines before and after the output. Can you make your program indent the output away from the left margin of the window?
  8. Commit all your code so far to your repo to make sure you've captured your properly working code.
Part Two
  1. IF YOU ARE ON A MAC WITH AN X86 PROCESSOR OR IF YOU ARE ON A WINDOWS COMPUTER you will need to download and install the nasm assembler from this location using the version that is appropriate for your operating system. For MacOS, use the macosx version, and you will need to download the zip file and do the installation manually. You will download a zip file. It is recommended that you move that file into a different directory, and NOT install nasm into your Downloads directory. You will need to add the installation directory into your PATH so that the O/S can find it.
    IF YOU ARE ON A WINDOWS MACHINE, use the win64 version, and download the .exe file, which is a self-extracting zip file; this will also run the installation process and [I think] add the installation directory to your search path.
    Either way, once that is done, verify that you can access nasm from your command line by running the command:
          nasm
                        

    If nasm is properly installed you should see an error message saying that you didn't provide a source file for nasm to work on.
  2. IF YOU ARE ON A MAC WITH A 'M-level' PROCESSOR YOU CAN SKIP THE NASM INSTALLATION. You will be using the  as assembler and the  ld loader to create your programs. These tools are already installed with the new MacOS versions. You can check this by running the following commands:
          as filename.arm
          ld filename
                        

  3. Now create a new file called sayhello.nasm in the same directory where your sayhello.c is located. For M-level Mac name the file sayhello.arm.
  4. Edit the file and paste in the following code:
    X86-64 Mac OS version [uses Mac X86-64 system calls]
             global      start                   ; defines the program entry point
    
             section     .text
    start:   mov         rax, 0x02000004         ; system call for write [Linux should be "_start"]
             mov         rdi, 1                  ; file handle 1 is stdout
             mov         rsi, message            ; address of string to output
             mov         rdx, 13                 ; number of bytes
             syscall                             ; invoke operating system to do the write
             mov         rax, 0x02000001         ; system call for exit [Linux use "60"]
             xor         rdi, rdi                ; exit code 0
             syscall                             ; invoke operating system to exit
    
             section     .data
    message: db          "Hello, World", 10
                        

    M-level Mac version [uses Mac M-level system calls]
             .global     _main                   ; declares the starting entry point
             .aligh      2
    
    _main:
             mov         X0,   #1                ; 1 = stdout, the display
             adr         X1,   message           ; address of start of message
             mov         X2,   #13               ; length of output string
             mov         X16,  #4                ; "4" is write system function
             svc         0                       ; call the system
    _exit:
             mov         X0,   #0                ; returns zero to O/S
             mov         X16,  #1                ; exit system function
             svc         0                       ; call the system
    
    message: .ascii      "Hello, World!\n"
                        

    Windows version [uses C library's 'printf' function]
             global      _main                   ; declares the starting entry point
             extern      _printf                 ; we'll use the "C" library for now
    
             section .text                       ; code starts here
    _main:   push        message                 ; put the message on the stack
             call        _printf                 ; call "printf()" to display it
             add         esp, 4                  ; set up the exit
             ret                                 ; return to Windows
    
    message: db  "Hello, World!", 10, 0
                        
  5. Assemble, link, and run with the following command:
    Mac OS X86-64 version
          nasm -fmacho64 sayhello.nasm
          ld -macosx_version_min 13.0.0 sayhello.o -o sayhello
          ./sayhello

    Mac OS M-level version
          as -o sayhello.o sayhello.arm
          ld -macosx_version_min 13.0.0 -o sayhello sayhello.o -lSystem -syslibroot `xcrun sdk macosx --show_sdk_path` -e start -arch arm64
          ./sayhello

    Windows version
          nasm -fwin32 sayhello.asm
          gcc sayhello.obj -o sayhello.exe
          sayhello
                        
  6. NOTE that the commands above are very specific, especially for the M-level. On Mac, run using ./sayhello and on Windows run using sayhello. If there are errors, do some Internet searching to see if you can track down what's going on. If you can't get some help from your friendly prof or a TA from the Keck Lab in Discord.
  7. Now that you have both the C and nasm versions working, see if you can figure out how to do the same dressing up of your assembler code, putting extra blank lines before and after the text output. Feel free to search the Internet for help. [Hint: check the message label in the code for clues!]
  8. When everything is working, be sure to commit your code to your repo!