Project Hello World

by: burt rosenberg
at: university of miami
date: aug 2019
NAME
    hello  -- prints "hello, world!\n" to standard out and exits.
    
SYNOPSIS
    hello
	
DESCRIPION
    Prints "hello, world!" to standard out and exits.

HISTORY
    Introduced in csc421.181.
    For csc421.211, program name and string exactly agrees with the text,
    The C Programming Language.
	

Goals

The goals of this project are:

Specific steps

Prepare an Ubuntu development image

  1. Install Virtual Box.
  2. Download ubuntu-16.04.6-server-i386.iso, the ISO (image of a CD or DVD) for ubuntu 16.04.6 server version, 32-bit (i386).
  3. Install the ISO on a Virtual Box virtual machine.
    1. Start Virtual Box, select New, and fill out the panel with any name, Linux, Ubuntu 32. (not really important)
    2. On the next several panels, select: 2G RAM; create virtual hard disk; VDI; dynamic; and 32G.
    3. You will be returned to the main panel, with the new image "Powered Off". Start it with the green arrow.
    4. You are prompted for the ISO image. Use the folder icon (small and to the right of the pull down) to navigate to your image, select and start.
    5. The purple screens are the Ubuntu installer. Take defaults. Use the TAB if the mouse does not yet work.
    6. Choose a machine name and a username.
    7. You will need to TAB (or mouse) choose YES to write the partition and format the filesystem. No proxy.
    8. Add Open SSH Server to the packages to install. Yes to install GRUB.
    9. Reboot and log in.
  4. Log into virtual ubuntu image (terminal) and update all software then install needed packages: sudo apt-get update
    sudo apt-get upgrade
    sudo apt-get install subversion
    sudo apt-get install build-essential
    reboot
Special notes:

Discussion

The provided hello.c is the template. Please modify to satisfy the test case. The Makefile provided has the default target to build the binary. The test target redirects output to the helloworld.out file and then compares the captured output to the reference output give in helloworld.ref. Differences will be reported. If the match, the diff program is silent.

The clean target removes build and test products. Never commit products! Commit only sources (in this project — Makefile, helloworld.c and helloworld.ref). Points off for committing build products.

See the original program on page 6 of The C Programming Language, by Kernighan and Ritchie.

Answer

        .file   "hello.c"
        .section        .rodata
.LC0:
        .string "hello, world!"
        .text
        .globl  main
        .type   main, @function
main:
        leal    4(%esp), %ecx
        andl    $-16, %esp
        pushl   -4(%ecx)
        pushl   %ebp
        movl    %esp, %ebp
        pushl   %ecx
        subl    $4, %esp
        subl    $12, %esp
        pushl   $.LC0
        call    puts
        addl    $16, %esp
        movl    $0, %eax
        movl    -4(%ebp), %ecx
        leave
        leal    -4(%ecx), %esp
        ret
        .size   main, .-main
        .ident  "GCC: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609"
        .section        .note.GNU-stack,"",@progbits
The answer is in the assembly language off of the i86 install in virtual box, using the line,
cc -S -fno-asynchronous-unwind-tables hello.c
This is presented as a discussion of how calls work, using the stack to remember the return address.

The pushl %ebp instruction is the equivalent of,

sub $4,%esp     # drop the stack 4 bytes
mov %ebp,(%esp) # move data onto the stack 
                # (indirect through the SP)
The call puts instruction equivalent is,
pushl %eip
movl  puts,%eip
where puts is the address where the puts subroutine starts.

The pushl $.LC0 is how the argument is passed to the subroutine. Note that the various assembler directives, that declare .LC0 to be the start adress of a string in a read-only data section.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.

author: burton rosenberg
created: 21 aug 2017
update: 22 aug 2020