Translate the following source code into a machine code using the assembly langu
ID: 3864042 • Letter: T
Question
Translate the following source code into a machine code using the assembly language of LMC and complete the table below. Var i=0; var j=1; var k=0; while(k<4) { var fib=i+j; i=j; j=fib; print(i); k=k+1; } 01 ? LDA 20 ;load k 02 ? ? ;k-4 03 717 ? ;Branch if equal to zero 04 ? ? ;load i 05 ? ? ; i+j 06 ? ? ;Store into fib 07 ? ? ;load j 08 ? ? ; Assign j to i (i=j) 09 ? ? ; load fib 10 ? ? ; Assign fib to j (j=fib) 11 ? ? ; load i 12 ? ? ; print into out basket 13 ? ? ;load k 14 ? ? ;k=k+1 15 ? ? ;store to k 16 ? ? ;load to the first 17 000 HLT ;end of the program 18 DAT 00 000 ;var i 19 DAT 00 001 ;var j 20 DAT 00 000 ;var k 21 DAT 00 100 ; const =4 22 DAT 00 000 ;fib=0 23 DAT 00 001 ;const =1
Explanation / Answer
Suppose we want a simple program that outputs pairs of numbers: the first number of each pair output will be a count value starting at 000 and stopping before reaching a MAX of 010; the second number of each pair will be double the value of the first number. In order to allow the user time to read the numbers output, the program will pause after each output for a certain amount of time (performing some time wasting loop). (Note that the program stops before reaching the MAX value; the MAX value is not output.)
We will code this as 3 separate source files: a MAIN program and two subroutines Pause() and Dble(). Here is the pseudocode for the three modules:
// MAIN program int count = 1 int max = 10 do { output count call Pause() call Dble(count) count += 1 } while ( count < max ); stop // PAUSE subroutine subroutine Pause() { int count = 0 do { count -= 1 } while ( count != 0 ); } // DBLE subroutine subroutine Dble(arg) { output arg+arg call Pause() }
Here are the three modules, translated in to LMC assembly language and then assembled into three object files:
Mnemonic Code
Object File - machine code and tables
; main program Repeat LDA Count ; output count OUT CALL Pause LDA Count STO Arg ; store subroutine argument CALL Dble LDA Count ; count += 1 ADD One STO Count SUB Max ; while ( count < max ) SKP JMP Repeat HLT Count DAT 000 One DAT 001 Max DAT 010 Pause EXT ; Pause is an External Arg EXT ; Arg is an External Dble EXT ; Dble is an External
00: 113 01: 600 02: 000 03: 113 04: 200 05: 000 06: 113 07: 314 08: 213 09: 415 10: 802 11: 900 12: 700 13: 000 14: 001 15: 010
Public Label
Location
(none, or possibly "MAIN")
External Reference
Location
Pause
02
Arg
04
Dble
05
Relocatable Address Locations
00, 03, 06, 07, 08, 09, 11
Mnemonic Code
Object File - machine code and tables
; Pause subroutine DAT ; CALL return address Pause LDA Count ; start of subroutine SUB One STO Count SKZ ; while ( count != 0 ) JMP Pause JMP Pause-1 Count DAT 000 One DAT 001 PUB Pause ; Pause is a Public Label
00: 000 01: 107 02: 408 03: 207 04: 801 05: 901 06: 900 07: 000 08: 001
Public Label
Location
Pause
01
External Reference
Location
(none)
Relocatable Address Locations
01, 02, 03, 05, 06
Mnemonic Code
Object File - machine code and tables
; Dble subroutine Arg DAT ; input argument to Dble DAT ; CALL return address Dble LDA Arg ; start of subroutine ADD Arg ; double the input value OUT CALL Pause JMP Dble-1 PUB Arg ; Arg is a Public Label PUB Dble ; Dble is a Public Label Pause EXT ; Pause is an External
00: 000 01: 000 02: 100 03: 300 04: 600 05: 000 06: 901
Public Label
Location
Arg
00
Dble
02
External Reference
Location
Pause
05
Relocatable Address Locations
02, 03, 06
External Reference
Location
Pause
02
Arg
04
Dble
05
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.