Difference between revisions of "CSC103 Assembly Language Lab 2014"

From dftwiki3
Jump to: navigation, search
(Translating/loading into RAM)
(Loading the Program into RAM)
Line 43: Line 43:
  
 
* Click on the '''Computer''' tab.
 
* Click on the '''Computer''' tab.
* Observe that all the instructions are there, written in purple, but that the actual contents of the memory are numbers.  Each instruction is coded as a number, and each number is stored at an address in memory.  The first instruction is always stored at Address 0.  The first instruction of the program is '''LOAD 1''', and '''LOAD''' is coded, as '''4''', stored at Address 0, and 1 is coded as... just '''1''', stored at the next address, i.e. Address 1.
+
* Observe that all the instructions are there, written in purple, but that the actual contents of the memory are numbers.  Each instruction is coded as a number, and each number is stored at an address in memory.  The first instruction is always stored at Address 0.  The first instruction of the program is '''LOAD 1''', and '''LOAD''' is coded, as '''4''', stored at Address 0, and 1 is coded as... just '''1''', stored at the next address, i.e. Address '''1'''.  The '''STORE''' instruction is stored at Address '''2''', and its argument, '''8''', at Address '''3'''.  The final instruction, '''HALT''' is stored at Address '''4'''.
  
 
==Execute the Progam==
 
==Execute the Progam==

Revision as of 21:37, 24 August 2014

--D. Thiebaut (talk) 21:48, 24 August 2014 (EDT)


This lab deals with assembly language and the Simple Computer Simulator (SCS). Work on this lab with a partner. It's easier to figure out some of these questions with another person. This lab uses numbers instead of variables when referring to memory.



There is an older version of this lab here.


Preparation

Start


SimpleComputerSimulator.png


Writing the Program

  • Click on the Program tab at the top of the simulator widget to open up the edit window.
  • Highlight the default program that appears in the window and delete it.
  • In the newly erased edit-window, enter this simple program:
LOAD 1
STORE [8]
HALT

You will notice that as you press the ENTER key after each line, the instruction is reformatted and a semicolon appended at the end. This is normal.

After you press the ENTER key after the last line, you should get this listing in the edit-window:

0000:    LOAD 1          ;
0002:    STORE [8]        ;
0004:    HALT            ;

Your program has been partly assembled. This is the term used to describe the translation that takes place when a collection of instructions gets transformed into numbers that will go into the memory of the computer. Here the numbers on the left of the colon represent the address in memory where each instruction will be saved.

Loading the Program into RAM

  • Click on the Computer tab.
  • Observe that all the instructions are there, written in purple, but that the actual contents of the memory are numbers. Each instruction is coded as a number, and each number is stored at an address in memory. The first instruction is always stored at Address 0. The first instruction of the program is LOAD 1, and LOAD is coded, as 4, stored at Address 0, and 1 is coded as... just 1, stored at the next address, i.e. Address 1. The STORE instruction is stored at Address 2, and its argument, 8, at Address 3. The final instruction, HALT is stored at Address 4.

Execute the Progam

Single Stepping

  • Click on the button that says "Set PC = 0" to make sure the processor will execute the program starting at 0
  • Click on the Cycle button and observe that the number 1 is going into the AC register and then into memory, at Address 10.

Running the program

  • If you want to run the program full speed rather that executing it a step at a time, the procedure is slightly different.
  • First, change the program so that it loads a number different from 1 into AC:
        lod-c    55
  • Translate
  • set PC = 0
  • Click Run instead of Cycle.
  • This runs the program.
  • Observe that 55 will be loaded up from memory into AC, and then stored into the variable var1, at Address 10.


You now know the basics of programming in assembly language.

Problem 1

  • Using the steps you have just gone through, write a program that initializes 3 memory locations at 10, 11, and 12 with the value 23. You should end up with the number 23 in three different memory locations.

Problem 2

  • Something very similar to the previous problem, but instead of storing the same value in all three variables, store 9, 19, 13 (this could be today's date) at Addresses 10, 11, and 12, respectively.

Problem 3

  • Try this program out:
@0 
        lod    10
	sto    11
	hlt

@10
   55
   0
  • Run it.
  • Observe the result, and in particular what happens to Memory Location 11.
  • Add two new variables to the data section at Addresses 12 and 13.
  • Modify the program so that it copies the contents of Address 10 into the locations at Addresses 11, 12, and 13.
  • Verify that your program works
  • Modify the number at Address 10 and restart the program. Does the program put the new value in the other 3 variables?

Problem 4

  • Try this program out:
@0
        lod    10
	add-c 	1
	sto    10
	hlt

@10
    55
    66
    3
  • Run the program
  • What does it do? What's the result of the computation?
  • Once you've figured it out, modify the program so that Address 10 originally starts with the value 5 in it, and then ends with the value 7.
  • Verify that your program will add 2 to the variable at Address 10, whatever its original value.
  • Once your programs works, modify the program so that it adds two to all three variables at Addresses 10, 11, and 12.

Problem 5

  • Try this new program out:
@0
       lod    10
	add-c	1
	sto    10
	jmp	   1


@10
    55
    55
  • Run the program. Study it.
  • Modify it so that as the program runs, the contents of Address 10 increases by 1 every step through the loop, and the contents of Address 11 decreases by one through the loop. In other words, when the program starts, Locations 10 and 11 contain 55. Then, a few cycles later, Location 10 becomes 56 and Location 11 54. Then a few cycles later, 57, and 53, etc.


Mini Challenge of the Day

QuestionMark3.jpg

Write a program that contains 4 variables starting at Address 10. The first 3 are initialized with 10, 20, and 30. The last one contains 0. Let's call the Location 10 var1, Location 11 var2, Location 12 var3, and Location 13 sum.

Your program should computes 3 * var1 + 2 * var2 + var3 and store the result into the variable sum. In our case, your program will compute 3*10+ 2*20+ 30 = 100 which it will store in sum.


Hints: If you look closely at the set of instructions for our processor, there is no multiplication instruction... So your solution has to use another method for computing the value of 3 * var1...








THE END!





































Solutions


P1

; Solution for Problem 1
; D. Thiebaut
start:	lod-c 23
	sto 10
	sto 11
	sto 12
	hlt

@10
0
0
0

P2

; Solution for Problem 2
; D. Thiebaut
start:	lod	10
	sto 11
	sto 12
	sto 13
	hlt

@10
55
0
0
0

P3

; Solution for Problem 3
; D. Thiebaut
start: 	lod    10
	add-c 	1
	sto     10
	lod	11
	inc
	sto	11
	lod	12
	add-c	1
	sto	12
	hlt

@10
55
66
3

P4

start: lod    10
	add-c     2
	sto    10
        lod    11
        add-c     2
        sto    11
        lod    12
        add-c     2
        sto    12
	hlt

@10
55
66
3

P5

; Solution for Problem 5
; D. Thiebaut
; First initialize both variables with 55

start:	lod-c  55
	sto		15
	sto		16

; now loop and increment var1 and decrement var2
; the loop is endless.

loop:	lod    		15
    	add-c		1
	sto    		15
	lod		16
	dec
	sto		16
	jmp		loop
	hlt

@15
0
0