CS 22300 - Programming Assignment 3

This assignment makes use of files contained in this zip file. This assignment is due Monday, November 28.

In an operating system, a "loader" is a program that loads other programs into the CPU's memory and then starts the execution of the newly loaded program. In this assignment you will use the TOY machine language to write a loader program that loads TOY's memory with a program read from TOY's standard input, and then runs the newly loaded code.

Write a TOY machine language program that reads numbers from TOY's standard input (memory address 0xFF) according to the following specification. The first number read in is the number of segments in the program that you are loading (in a real loader, the segments have names like .bss, .text, .data, .rodata). The second number read in is the beginning address in TOY's memory where the first segment's data should be loaded. The third number is the length of the segment (the number of words in the segment). That number is followed by the actual data for the segment and your program should read each number and store it in the appropriate memory location. After reading all the numbers in the segment, the next number from standard input is the beginning address of where to load the second segment (if there is one). The number after that is the length of the second segment. That number is followed by the data for the second segment. This pattern holds for each of the segments. After all of the segments have been read in from TOY's standard input, the last number your program should read in is the "entry point" of the program you just loaded. The entry point is the address where the TOY cpu should start executing the newly loaded program. After your loader reads in the number for the entry point, your program should execute a JMP instruction to that address.

For example, consider this data.

    0002
    0020
    0005
    0001
    0002
    0003
    0004
    0005
    0030
    0002
    0050
    0051
    0020

It describes a program with two segments. The first segment should be loaded starting at address 0x20 and it has five words of data. The second segment should be loaded starting at address 0x30 and it has two words of data. After the data is loaded, execution should begin at address 0x20.

The TOY simulator program has a nice feature where it lets you load and save TOY's standard input data stream as a file. If you put the simulator in either edit mode or debug mode, and you go to the tab called "Stdin", you should see "Open..." and "Save as..." menu items. They can be used to initialize TOY's standard input data stream from a file and save a modified data stream to a file. In the zip file are a couple of saved standard input data streams that represent programs your loader can load (the file toy_stdin_2.txt contains data for loading and executing the sample program reverse.toy).

Included in the zip file is a command-line version of the TOY simulator. You run it with the following command.

       c:\> java TOY  --verbose  loader.toy < toy_stdin_2.txt

When you write your TOY code, you should use the three column format used in the example programs. The first column is the actual machine language. The second column is the equivalent assembly language. Use the third column for (English) comments. (In the examples that come with TOY, the third column is often a high level language equivalent of the assembly language. If you want, you can try to translate your assembly code into C like code, but you don't have to do this step.)

Machine code, and even assembly code, is inscrutable. So in addition to the TOY source file, loader.toy, write a text file loader.txt that explains in detail how your loader program works. For example, describe what role each register plays in your program. Explain the looping structure of your code. Your explanation should be a mixture of English with highly commented and annotated assembly code from loader.toy (in particular, your explanation should include all the assembly code).

Turn in a zip file called CS223Hw3Surname.zip (where Surname is your last name) containing your version of loader.toy and your explanation, loader.txt, of your machine language program.

This assignment is due Monday, November 28.