Comp3302 Session 9 Tasksheet

C.B.Price November 2016

Purpose / To investigate a minimal Turing-Complete programming language.
Files Required / StudentTemplate.zip of Atomic_Language_HLL.zip (Visual Studio Solutions)
PP Contribution / This could contribute to both the options for Position Paper 3.
Send to Me / Nothing this week.
Homework / Work on Position Paper 3

Activities (Choose either A or B)

A.1 / Here we shall investigate a minimal set of Pentium assembler instructions which can be used to create any program. First set up Visual Studio.
1. Copy the folder “StudentTemplate.zip” to your desktop or place of choice and unzip
2. Double left-click on the file Solution1.sln to open it in Visual Studio
3. In the Solution Explorer expand the AsmTest tab then expand Source Files and double-left click on Student_Template.asm. This assembler file contains a few instructions. It is here that you will soon be writing your assembler code. Note the red ball, which is a breakpoint to halt the execution of the program.
4. Hit the Build button on the menu bar and choose Build Solution.
5. Select Debug from the menu bar, then choose Start Debugging.
6. On your first run, select Debug again and then choose Windows and select Registers.
7. Step through the code by pressing F11 and keep an eye on the registers window as you step. When a register is changed on each step, it will be highlighted as red.
A.2 / Get to know some assembler instructions.
Write some short programs using various combinations of the following instructions and observe how the CPU registers change
moveax,N where N is an integer
inceax
deceax
add eax,ebx
A.3 / Now assume you have an instruction set which contains only the following instructions and which works on four registers eax, ebx, ecx, edx.
increg / e.g. inceax. Increments the value in the register by 1
decjmpregreg, Label / If regis 0 then jump to Label, else decrement reg by 1 and move to the next instruction
(a) Write code to assign a number to eax (assume that eax was initially 0).
(b) Write a program to set eax to zero
(c) Write a program to copy the contents of eax into ebx
(d) Write a program to add the contents of eax and ebx and put the result into ecx.
B.1 / Here we shall investigate “Dr.C’s Conjecture” that any program can be written using just three “Atomic Instructions”. First, let’s set up Visual Studio.
1. Copy the folder Atomic_Language_HLL.zip to a place of your choice and unzip.
2. Double left-click on the file Atomic_Language_HLL.sln to open it in Visual Studio.
3. In Solution Explorer, expand the Atomic_Language_HLL tab and then expand the Source Files folder.
4. Double left-click on Atomic_Language_HLL.cpp. You will see a few lines of code, to declare a variable, to assign a variable and to print it out to the console.
5. Compile the code: from the menu bar Build > Build Solution
6. Run your code: from the menu bar Debug > Start Without Debugging. You will get a black “console” window showing the result of your printf statement.
Now assume the following “Atomic Instructions”
(i) You can declare as many variables (type int) as you need.
(ii) There are only two operations on your variables: var++; and var--; which increment and decrement the var by 1.
(iii) The statement below which repeats whatever is between the curly brackets when vari is less than num.
while(varinum) {

}
Note you cannot use assignments like a = 3; b = a;
B.2 / (a) Assume a variable has a value (e.g. a = 3;) Now use the while loop to set it to zero.
(b) Assume a variable is zero. Now use the atomic instructions to assign a value (e.g. 3) to a variable.
From now on, you know how to code an assignment (e.g. a = 3;) using atomic instructions. So you are allowed to use this assignment. Also, you will need to use the while-loop in much of what follows.
(c) Work out how to code the assignment b = a;
(d) Work out how to add two variables like this b = b + a;
(e) You should find it straightforward to change your solution to subtract b = b – a;
B.3 / Selection Statements – We do not need the if-then-else!
(a) Let’s say we wish to code if(x < 5) y = 1; The following code snippet will do this. Try it out.
y = 0;
while(x < 5) {
x = 5;
y = 1;
}
(b) Think how to extend this to code if(x < a) y = 1; and write code to do this.
(c) Can you work out how to code this if( (x < a) & (x > b) ) y = 1;
(d) And finally this if(x < 1) y=1; else y=2;