examples

Page 1

MARIE Program Examples (1)

X, Y,

Load X Subt Y Skipcond 800 Store Y Halt Dec 200 Dec 300

//skip if X > Y

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:

X, Y, •

Load X Subt Y Skipcond 800 Store Y Halt Dec 200 Dec 300

Location 0 1 2 3 4 5 6

Translated Machine Code (Binary): 0001 0100 1000 0010 0111 0000 0000

000000000101 000000000110 100000000000 000000000110 000000000000 000011000100 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.


(2)

S1, X, Y, •

(3)

Input Store Y Input Store X Subt Y Skipcond 800 Jump S1 Load X Output Halt Load Y Output Halt Dec 0 Dec 0

//skip if X > Y // Y = max(X,Y) // X = max(X,Y)

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

Input Store X Input Add X Subt Offset Output Halt X, Dec 0 Offset, 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’).


(4)

S1,

Input Subt Offset Add X Store X Load Count Subt One Store Count Skipcond 400 Jump S1 Halt X, Dec 0 Offset, Dec 48 Count, Dec 10 One, Dec 1 •

(5)

//if AC = 0 then terminate

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.

S1,

C, •

//input ASCII digit //convert it into integer I; AC = I //AC = AC + [X] //[X] = I //AC = [Count] //AC = [Count] - 1 //[Count] = AC = [Count] - 1 //AC = 0?

Input Output Subt C Skipcond 400 Jump S1 Halt Dec 48

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


(6)

S1, R,

X, Y, • • •

• •

7.

Input Jns R Jump S1 Dec 0 Store Y Subt X Skipcond 800 JumpI R Load Y Store X JumpI R Dec 0 Dec 0

//Jump to subroutine at R //this is the return address //this stores the return address (refer to Jns) //this the first instruction of the subroutine //Y > X? //if not, ‘return control’ to the return address //else update X = Y //return control to the return address

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.

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

S, Load Sum AddI X Store Sum Load X Add One Store X Load C Subt One Store C Skipcond 400 Jump S


Halt Sum, Dec One, Dec C, Dec X, Dec Dec Dec Dec

0 1 3 17 1 2 3 Dec

4


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.