Fahrenheit

Table 1.
# Label Memory Address Binary value stored at address Interpretation
# 0x0000 0000 0000 0000 0000 0000 0000 0000 0000 0000...
# ... ... Text Segment
# 0x0000 FFFC0000 0000 0000 0000 0000 0000 0000 0000...
# 0x0001 0000 0000 0000 0000 0000 0000 0000 0000 0000"%3d "
# 0x0001 0000 0000 0000 0000 0000 0000 0000 0000 0000"%6.1"
# 0x0001 0000 0000 0000 0000 0000 0000 0000 0000 0000"f\n"
# 0x0001 FFFC 0000 0000 0000 0000 0000 0000 0000 0000...
# ... ...
# 0x0001 FFFC0000 0000 0000 0000 0000 0000 0000 0000...
# ... ...
# ... ...
#celcius: 0xFFFF EFF00000 0000 0000 0000 0000 0000 0000 00000
#fahrenheit: 0xFFFF EFF4 1111 1111 1111 1111 1111 1111 1111 ????0
#format: 0xFFFF EFF8 0000 0000 0000 0000 0000 0000 0000 00010x0001 0000
# 0xFFFF EFFC 0000 0000 0000 0000 0000 0000 0000 0000Return value
#myprintf 0xFFFF EFF0 0000 0000 0000 0000 0000 0000 ???? ????Return address
#fahr: 0xFFFF EFF00000 0000 0000 0000 0000 0000 0000 00000
#argv: 0xFFFF EFF4 1111 1111 1111 1111 1111 1111 1111 ????0xFFFF FFF4
#argc: 0xFFFF EFF8 0000 0000 0000 0000 0000 0000 0000 00010x0001 i.e., int argc
# 0xFFFF EFFC 0000 0000 0000 0000 0000 0000 0000 0000Return value
#main 0xFFFF EFF0 0000 0000 0000 0000 0000 0000 ???? ????Return address
# ... ...
#*argv[0] 0xFFFF FFE00010 1110 0100 0111 0110 1000 0110 01010x2E47 6865 "./fa"
# 0xFFFF FFE40110 1100 1100 0110 0110 FFFF 0000 00000x6C6C 6F00 "hren"
#*argv[1] 0xFFFF FFE80010 1000 0110 0101 0110 1100 0110 11000x4865 6C6C "heit"
# 0xFFFF FFEC0110 1111 0111 1111 0101 0111 0110 11110x6F7F 576F "\0"
#argv: 0xFFFF FFF41111 1111 1111 1111 1111 1111 1110 00000xFFFF FFE0
# 0xFFFF FFFC0000 0000 0000 0000 0000 0000 0000 00000x0000 0000
$ ./fahrenheit

fahrenheit.c

#include 
/*print Fahrenheit-Celsius table */
main()
{
    int fahr;

    for (fahr = 0; fahr <= 300; fahr = fahr + 20)
        myprintf("%3d %6.1f\n", fahr, (5.0/9.0)*(fahr-32));
}

Approximation of text segment

cruntime:
    0x???? ????
    0                        ;   Setup stack for main call
                             ;   Gonna need 1 for return address, 1 for return value, 1 for argc, 1 for argv.
                             ;   Push values for argc and argv on stack
    0x???? ????              ;   CALL main - a jump to main label
    0x???? ????              ;   Finalise and exit

main:
    0x???? ????              ;   Make room on stack for 'fahr'. (int).
    0x???? ????              ;   SET $R2 = 0
    0x???? ????              ;   STORE from $R2 into fahr
branch:
    0x???? ????              ;   BRANCH TO here if $R2 is not less than equal to 300
    0x???? ????              ;      Make room on stack for 'myprintf' - return address/value, format, fahrenheit, celcius
    0x???? ????              ;      Store 0x0001 0000 into format
    0x???? ????              ;      STORE from $R2 into fahrenheit
    0x???? ????              ;      SET $R3 = .55555
    0x???? ????              ;      SET $R4 = $R2 - 32
    0x???? ????              ;      SET $R4 = $R4 * $R3
    0x???? ????              ;      STORE from $R4 into celcius
    0x???? ????              ;      CALL myprintf - a jump to myprintf label
    0x???? ????              ;      SET $R2 = $R2 + 20
    0x???? ????              ;      STORE from $R2 into fahr
    0x???? ????              ;   JMP to branch
here:
    0x???? ????              ;   RET
    0x???? ????
myprintf:
    0x???? ????
    0x???? ????

References