
Accumulator Machine Simulator Developed for CS2700 Assembly Language and Computer Architecture Xuejun Liang Fall 2009 Motivation • MARIE is an accumulator-based machine simulator • Used for assembly language programming exercises. • But, MARIE simulator has some problems: • Programmers can not define a variable to hold the address of another variable. • Conditional branch can only skip the next instruction. It takes numbers as its operands for indicating different conditions. Programmers have to remember these numbers. • MARIE do not have the stack pointer. So its subroutine has no local variables and can not be recursive. • To solve these problem, I developed a new accumulator- based machine simulator. The goal of this simulator • Used for assembly language programming with an accumulator-based machine. • Microarchitecture that will support the execution of instructions of this accumulator-based machine is not considered for now. • Used for advanced course projects • Add more instructions in the simulated machine • Add user interfaces for the simulator • Add more debugging functions in the simulator • Design a microarchitecture to support the simulated machine. Architecture Characteristics • Binary, two's complement data representation. • Stored program, fixed word length data and instructions. • 64K words of word-addressable data memory. • 64K words of word-addressable instruction memory. • 32-bit data words. • 32-bit instructions. • A 32-bit arithmetic logic unit (ALU). • Only arithmetic operations are implemented • Registers • ACC: 32-bit Accumulator • PC: 32-bit Program counter • SP: Stack pointer pointing to the top of the stack op Instruction Explanation Instruction Set 0 LIMM Imm ACC Imm 1 AIMM Imm ACC ACC+Imm 2 ADD Var ACC ACC+M[Var] Imm: 16-bit 2’s compliment 3 SUB Var ACC ACC-M[Var] ACC: Accumulator 4 MUL Var ACC ACC*M[Var] PC: Program counter 5 DIV Var ACC ACC/M[Var] Var: Variable 6 REM Var ACC ACC%M[Var] Lab: Label 7 GET Var ACC M[Var] M[A]: Memory content of variable A 8 PUT Var M[Var] ACC PUSH PC: Push PC on stack 9 GOTO Lab PC Lab 10 BEQZ Lab If ACC = 0 GOTO Lab POP: Remove top content on stack 11 BNEZ Lab If ACC ≠ 0 GOTO Lab SP: Reserved location, Stack pointer 12 BGEZ Lab If AC퐶 ≥ 0 GOTO Lab ZERO: Reserved location, M[ZERO]=0 13 BLTZ Lab If ACC < 0 GOTO Lab 14 JNS Lab PC L & PUSH PC $+Imm: Local variable 15 JR PC M[M[SP]] & POP Its address is M[SP]+Imm, 16 READ ACC Input where Imm is a 16-bit integer. 17 PRNT Print ACC Example: ADD $+4 means 18 STOP Stop ACC ACC + M[M[SP]+4] 19 GETI Var ACC M[M[Var]] 20 PUTI Var M[M[Var]] ACC Pseudo-Instructions Pseudo- Meaning Instruction instruction 1 POP M[SP] = M[SP] -1 GET SP AIMM -1 PUT SP 2 TOP A M[A] M[M[SP]] GETI SP PUT A 3 PUSH A M[SP] = M[SP] + 1 GET SP M[M[SP]] M[A] AIMM 1 PUT SP GET A PUTI SP Implementations • Three separate 32-bit integer arrays are used for instructions, memory data, and input data, respectively. • Each instruction takes 22 bits • 5-bit opcode, and 16-bit operand (address). • 1-bit indicating if the operand is local or global • But, using one 32-bit word to hold one instruction. • Instruction memory address is 16 bits. So, Instructions will take up to 64K 32-bit words (or integers). • The branch instructions use 16-bit absolute address. The instructions JNS also use 16-bit absolution address • Each datum occupies 32 bits. The data address is 16 bits. So, we have 64K words of data memory. • Stack is growing towards higher data memory address Program Structure and Syntax (1) • Every program contains three sections [Data] and separated by END END • Data (optional) Code • Code END • Input (optional) [Input] • Data (Declarations) • One variable definition per line • ID (identifier) is the variable name. ID Type [Value] • Type is a positive integer • Type = 1, ID is a scalar variable • Type > 1, ID is an array variable [Label:] Instruction • Value is up to Type initial integers of ID. If less than Type initial values are provided, Number (integer) default initial values are used. Program Structure and Syntax (2) • Code (Instructions) • One instruction per line [Data] • Label is optional. It must be followed by ‘:’ END immediately. There is no space between Code Label and ‘:’. END • Instruction is any instruction, including [Input] pseudo-instruction. • Input: ID Type [Value] • One input value per line. • Number is any integer. • Comments: [Label:] Instruction • Any text starting from // to the end of the line will be considered as comments Number (integer) Simple Example 1: Add Two Numbers Our Simulator Code MARIE Simulator Code X 1 23 //First number Load X /Get first number Y 1 48 //Second number Add Y /Add the second Z 1 0 //Sum Store Z /Store Sum at Z END Output /Print the Sum GET X //Get first number Halt /Terminate program ADD Y //Add the second X, DEC 23 /First number PUT Z //Store Sum at Z Y, DEC 48 /Second number PRNT //Print the Sum Z, DEC 0 /Sum STOP //Terminate program END Example 2: Compute sum of absolute values of elements in an array Our Simulator Code //Data I 1 0 //Array Index SUM 1 0 //Sum N 1 9 //Number of elements in the array TMP 1 0 //Temporary location PDAT 1 DAT //A pointer to the array DAT DAT 9 10 20 30 -40 50 60 70 80 -90 //array DAT END //Code L1: GET N SUB I BEQZ L3 //if (N-I)=0, done GETI PDAT //Get an array element into ACC Our Simulator Code BGEZ L2 //if positive, skip PUT TMP //else, negate LIMM 0 SUB TMP L2: ADD SUM //add to sum PUT SUM GET I //increase index I by one AIMM 1 PUT I GET PDAT //increase array address by one AIMM 1 PUT PDAT GOTO L1 //next element L3: GET SUM //print sum PRNT STOP //stop END //Inputs //None Example 3 MARIE Assembly Program: Ex4_2.mas in the MARIE simulator package If X = Y then X = X × 2 else ORG 100 Y = Y - X If, Load X /Load the first value Subt Y /Subtract the value of Y, store result in AC Skipcond 400 /If AC=0, skip the next instruction Jump Else /Jump to Else part if AC is not equal to 0 Then, Load X /Reload X so it can be doubled Add X /Double X Store X /Store the new value Jump Endif /Skip over the false, or else, part to end of if Else, Load Y /Start the else part by loading Y Subt X /Subtract X from Y Store Y /Store Y-X in Y Endif, Halt /Terminate program (it doesn't do much!) X, Dec 12 /Load the loop control variable Y, Dec 20 /Subtract one from the loop control variable END Example 3 Our Simulator Code If X = Y then X = X × 2 else X 1 12 Y = Y - X Y 1 20 END If: GET X //Load the first value SUB Y //Subtract the value of Y, store result in AC BNEZ Else //If AC=0, Jump to Else part Then: GET X //Reload X so it can be doubled ADD X //Double X PUT X //Store the new value GOTO Endif //Skip over the else part to end of if Else: GET Y //Start the else part by getting Y SUB X //Subtract X from Y PUT Y //Store Y-X in Y Endif: STOP //Terminate program END Example 4: Compute Fibonacci Number fib(N), Where N is an Input Mathematics formula int I, A, B, C, N cin >> N; C++ code using a loop if (N < 2) C = N; else { A = 0; B = 1; for (I = 2; I <= N; I++) { C = B + A; A = B; B = C; } } cout << C; Example 4: C++ Code with Using Function int N; int N; int fib(int N) { int fib(int N) { int I, A, B, C; if (N < 2) if (N < 2) return N; return N; else else { return f(N) + f(N-1); A = 0; B = 1; } for (I = 2; I <= N; I++) { cin >> N; C = B + A; A = B; B = C; C = fib(N); } cout << C; } return C; } Recursive function cin >> N; C = fib(N); Non-recursive function cout << C; Example 4: L1:GET B //ACC=B ADD A //ACC=B+A Loop Solution PUT C //C=B+A GET B //ACC=B //Declarations PUT A //A=B I 1 1 //index GET C //ACC=C N 1 0 //N PUT B //B=C C 1 0 //f(N) GET I //ACC=I B 1 1 //f(N-1) AIMM 1 //ACC=I+1 A 1 0 //f(N-2) PUT I //I=I+1 END SUB N //ACC=I-N //Instructions BLTZ L1 //if I<N, cont. READ //Read input L2:GET C //print result PUT N //N=input PRNT PUT C //C=N STOP //stop SUB I //ACC=N-1 END BLTZ L2 //if N<1 //inputs BEQZ L2 //if N=1 10 //N Example 4: Using Function Stack Frame or Activation Record N 1 0 //N: input C 1 0 //C: result fib(N) Addr Name Explanation END //main code $-1 N/Fib(N) Input/output READ //Read input $ RA Return Address PUT N //N=input $+1 I Local variable PUSH N //Push N on stack JNS Fib //call Fib $+2 A Local variable TOP C //Get and save fib(N) $+3 B Local variable POP //Restore stack $+4 C Local variable GET C //Get fib(N) from C PRNT //Print fib(N) STOP L2: GET $+3 //ACC=B ADD $+2 //ACC=B+A PUT $+4 //C=B+A Fib: LIMM 1 GET $+3 //ACC=B PUT $+1 //I=1 PUT $+2 //A=B PUT $+3 //B=1 GET $+4 //ACC=C AIMM -1 PUT $+3 //B=C PUT$+2 //A=0 GET $+1 //ACC=I GET $-1 //ACC=N AIMM 1 //ACC=I+1 SUB $+1 //N-1 PUT $+1 //I=I+1 BLTZ L3 //N<1 SUB $-1 //ACC=I-N BEQZ L3 //N=1 BLTZ L2 //if I<N, cont.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages22 Page
-
File Size-