examples

8

Click here to load reader

description

MARIE Program Examples Assembler Source: Location Translated Machine Code (Binary): Some Explanations:

Transcript of examples

Page 1: examples

MARIE Program Examples

(1) Load XSubt YSkipcond 800 //skip if X > YStore YHalt

X, Dec 200Y, Dec 300

Some Explanations:

X, Y are symbolic addresses created by the programmer. These define memory locations via the pseudo-op (Dec), directing the MARIE assembler to initialize the contents of these locations with values (decimal digits 200 and 300 respectively).

Upon translation, the machine language program includes 7 memory locations, the first five locations contain instructions and the remaining two locations contain the initialized data (200 and 300).

The assembler translates each symbolic (assembly language) instruction or assembler pseudo-ops into its binary equivalent, the machine language code.

Assembler Source: Location Translated Machine Code (Binary):

Load X 0 0001 000000000101Subt Y 1 0100 000000000110Skipcond 800 2 1000 100000000000Store Y 3 0010 000000000110Halt 4 0111 000000000000

X, Dec 200 5 0000 000011000100Y, Dec 300 6 0000 000100101100

In the above, the left-hand-side column is the assembly language program (source). During translation, the assembler (translator) identifies the corresponding memory locations for each line of the source by scanning it in one pass. As a result of this pass, the assembler knows X corresponds to memory location 5 and Y corresponds to memory location 6. Then in the second pass, it translates each line (symbolic opcode and symbolic location) into the binary form.

The simplicity of MARIE makes this translation straightforward. In practice, there can be more complex issues that are beyond the scope of this course.

Page 2: examples

(2) InputStore YInputStore XSubt YSkipcond 800 //skip if X > YJump S1 // Y = max(X,Y)Load X // X = max(X,Y)OutputHalt

S1, Load YOutputHalt

X, Dec 0Y, Dec 0

The above program inputs two ASCII characters, and outputs the ‘larger’ ASCII character before halting.

(3) InputStore XInputAdd XSubt OffsetOutputHalt

X, Dec 0Offset, Dec 48

The above program inputs two ASCII characters (possibly decimal digits), computes the sum of these two digits, converts the result properly into an ASCII digit (by subtracting 48 from it) before outputting the result digit, assuming the result is a single digit (else ‘overflow’).

Page 3: examples

(4) S1, Input //input ASCII digitSubt Offset //convert it into integer I; AC = IAdd X //AC = AC + [X]Store X //[X] = ILoad Count //AC = [Count]Subt One //AC = [Count] - 1Store Count //[Count] = AC = [Count] - 1Skipcond 400 //AC = 0?Jump S1Halt //if AC = 0 then terminate

X, Dec 0Offset, Dec 48Count, Dec 10One, Dec 1

The first two instructions input an ASCII digit and convert it into integer I. The iterative loop (from the first instruction to Jump S1) body adds the integer to an accumulated sum (stored in X). With the Count initialized to 10, the loop is executed exactly 10 times before it halts.

Upon termination, X contains the sum of the ten digits received from the input.

(5) S1, InputOutputSubt CSkipcond 400Jump S1Halt

C, Dec 48

The above program repeatedly inputs an ASCII character and echoes it to the output. It terminates when the input character is ‘0’.

Page 4: examples

(6) S1, InputJns R //Jump to subroutine at RJump S1 //this is the return address

R, Dec 0 //this stores the return address (refer to Jns)Store Y //this the first instruction of the subroutineSubt XSkipcond 800 //Y > X?JumpI R //if not, ‘return control’ to the return addressLoad Y //else update X = YStore XJumpI R //return control to the return address

X, Dec 0Y, Dec 0

The above program repeatedly inputs an ASCII character, calls the subroutine R that retains the maximum character received so far.

The subroutine includes the fourth line onward to the end in the above code.

Notice that JumpI R allows the control to return to an arbitrary return address saved at memory location R by the calling instruction (Jns) whenever the latter is executed. This is called control linkage, the linking of control between a caller and the called subroutine.

Different callers (instances of Jns instructions in a program) may leave different return addresses at R at runtime.

MARIE uses a static location (referred explicitly by the Jns instruction) for storing the return address. In the example, R is the control linkage location. Since only one return address can be saved in this location, the subroutine must return control to its caller before another caller can call the same subroutine again. This forbids recursive calls (that involve a subroutine calling itself directly or indirectly). You can imagine what problem will arise if the subroutine R contains a Jns R instruction within its body.

7. Sum of an array where the starting address is in memory location X and the count is in C.

S, Load SumAddI XStore SumLoad XAdd OneStore XLoad CSubt OneStore CSkipcond 400Jump S

Page 5: examples

HaltSum, Dec 0One, Dec 1C, Dec 3X, Dec 17Dec 1Dec 2Dec 3

Dec 4