1.15k likes | 1.37k Views
Example of SIC assembler language p r ogram Line source statement. 1/4. Example of SIC assembler language p r ogram. 2/4. 409 6 byt e 的暫存區. 115 . 120 . SUBROUTINE TO READ RECORD INTO BUFFER (呼叫副程式讀取紀錄到暫存區). ZERO ZERO. 清除迴圈計數器 把AX暫存器設為0. 125 RDREC LDX 130 LDA.
E N D
Example of SIC assembler language program Linesource statement 1/4
Example of SIC assembler language program 2/4 4096byte 的暫存區 115 . 120 . SUBROUTINE TO READ RECORD INTO BUFFER (呼叫副程式讀取紀錄到暫存區) ZERO ZERO 清除迴圈計數器 把AX暫存器設為0 125 RDREC LDX 130 LDA
Assembler language program (Fig.2.1) Algorithm of assembler (Fig.2.4) Objectprogram Assembly listing for (Fig.2.3) debugging(Fig.2.2)
5 COPY START 1000 Fig.2.1 begin read firstinputline ifOPCODE='START' thenbegin save#[OPERAND]asstartingaddress initialize LOCCTR to starting address writelinetointermediatefile read nextinputline end {ifSTART} else initialize LOCCTR to 0 Fig.2.4(a) COPY START LOCCTR=1000 1000 Intermediate file
FIRST STL RETADR 10 Fig.2.1 whileOPCODE<>'END' do begin . . insert (LABEL.LOCCTR) into SYMTAB . iffoundthen add 3 {instruction length} to LOCCTR . . writelinetointermediatefile readnextinputline end{while} Fig.2.4(a) (FIRST,1000) FIRST STL RETADR LOCCTR=1003 Intermediate file
CLOOP JSUB RDREC 15 Fig.2.1 whileOPCODE<>'END' do begin . . insert (LABEL.LOCCTR) into SYMTAB . iffoundthen add 3 {instruction length} to LOCCTR . . writelinetointermediatefile readnextinputline end{while} Fig.2.4(a) (CLOOP,1003) CLOOP JSUB RDREC LOCCTR=1006 Intermediate file
LDA LENGTH 20 Fig.2.1 whileOPCODE<>'END' do begin . . if found then add 3 {instruction length} to LOCCTR . writelinetointermediatefile readnextinputline end{while} Fig.2.4(a) LOCCTR=1009 LDA LENGTH Intermediate file
80 85 90 EOF THREE ZERO BYTE C’EOF’ WORD 3 WORD 0 Fig.2.1 whileOPCODE<>'END' do begin . insert(LABEL.LOCCTR) into SYMTAB . elseifOPCODE='WORD'then add3toLOCCTR elseifOPCODE='BYTE'then begin find length of constant in bytes add length to LOCCTR end . writelinetointermediatefile readnextinputline end{while} Fig.2.4(a) (EOF , 102A) EOF BYTE C’EOF’ WORD 3 WORD 0 (THREE ,102D) THREE LOCCTR=1030 (ZERO , 1030) LOCCTR=1033 ZERO Intermediate file
Function of algorithm for pass_1 of assembler (1)Assign address to all statements in the program (2)Save the values (address) assigned to all labels (3)Perform some processing of assembler directives
. (RETADR , 1033) . 5 10 COPY FIRST START STL 1000 RETADER intermediate file Pass2: begin read first input line {from intermediate file} if OPCODE = 'START' then begin … end{ifstart} write Header record to obect program initialize first Textrecord while OPCODE≠'END' do ……….. write listing line read next input line end{while} write last Text record to object program … end{pass2} Fig2.4(b) 5 10 HCOPY--00100000107A 1000 COPY START 1000 1000 FIRST STL RETADR T001000141033 141033 Fig2.2
Assembler language program (Fig.2.1) Algorithm of assembler (Fig.2.4) Objectprogram Assembly listing for (Fig.2.3) debugging(Fig.2.2)
Object Program correspond to Fig 2.2 case1 (LOCCTR-starting address) =length H COPY 001000 00107A
Object Program correspond to Fig 2.2 case2 LineLoc10 1000 Sourcestatement FIRST STL RETADR Pass 2 Fig2.4(b) Instruction Table SYMTAB ( LABEL LOCCTR ) RETADR 1033 141033
Object Program correspond to Fig 2.2 case3 character Pass 2 Fig2.4(b) elseifOPCODE='BYTE' or 'WORD' then convertconstanttoobjectcode 454F46
Object Program correspond to Fig 2.2 case4 Pass 2 Fig2.4(b) else if OPCODE = 'BYTE' or 'WORD' then convertconstanttoobjectcode 000003
Object Program correspond to Fig 2.2 case5 LineLoc185205D SourcestatementINPUT BYTE X’F1’ hexadecimal Pass 2 Fig2.4(b) elseifOPCODE='BYTE' or 'WORD' then convertconstanttoobjectcode F1
Object Program correspond to Fig 2.2 case6 LineLoc190205E SourcestatementMAXLEN WORD 4096 Pass 2 Fig2.4(b) else if OPCODE = 'BYTE' or 'WORD' then convertconstanttoobjectcode 4096=212 12841 001000
Object Program correspond to Fig 2.2 case7 SYMTAB Pass 2 Fig2.4(b) ( LABEL LOCCTR ) FIRST 1000 E001000
Object Program correspond to Fig 2.2 HCOPY 00100000107A T 001000 1E 141033 482039 001036 281030 301015 482061 3C100300102A0C103900102D T 00101E 15 0C1036 482061 081033 4C0000 454F46 000003 000000 T 002039 1E 041030 001030 E0205D30203FD8205D281030 3020575490392C205E38203F T 002057 1C 101036 4C0000 F1 001000041030E02079302064 509039DC20792C1036 T 002073 07 382064 4C0000 05 E 001000 Fig 2.3
2.2 Machine-dependent assembler 2.2.1 Instruction Formats and Address Modes START statement now specifies beginning program address of 0. As we discuss in the next section, thisindicates a relocatableprogram.
2.2 Machine-dependent assembler (1)Addressingmodesymbols: @:indirectaddressingmode 70 J @RETADR 95 RETADR RESW 1 # : immediate addressing mode 55 LDA #3 + : extended instruction format 15 CLOOP +JSUB RDREC
2.2 Machine-dependent assembler (2)Useof register-register instructions instead of registermemory instructions ->improvetheexactionspeedofthe program. CPU Memory I/O
2.2 Machine-dependent assembler • The conversion of register mnemonics to numbers can be done with a separate table; however, SYMTAB would be preloaded with the register names (A,X, etc.) and their values(0,1,ects.). • Most of the register-to-memory-instructions are assembled using either program-counter relative or base relative addressing. • Fit in the 12-bit field in the instruction • Displacement must be between 0 and 4095(for base) orbetween -2048 and 2047(for program-counter).
2.2 Machine-dependent assembler (3)Ifneitherprogram-counterrelativenor base relative addressingcanbeused,then the4-byteextendedInstructionformatmustbeused. 15 0006 1036-0009 =102D >1000 125 1036 CLOOP +JSUB . . RDREC RDREC CLEAR X
2.2 Machine-dependent assembler The extend format by using the prefix + (as on line 15).addressing is applicable and extended format is not specified, then the instruction cannot be properly assembled. In this case, the assembler must generate an error message.
2.2 Machine-dependent assembler (4)Displacementcalculationforprogram- counterrelative and base addressing modes: 10 0000 FIRST STL RETADR Since address (RETADR) =0030 and next address (FIRST) =0003, we obtain displacement=0030-0003=02Dwith pc relative addressing and neither indirect nor immediate addressing,theobjectcodeofthisassemblyinstructionis 17202D n i x b p e .. 11 0 0 1 0 .. Opcode (STL) 000101 1 7 2
2.2 Machine-dependent assembler • The program counter is advanced after each instruction is fetched and before it is executed. • The displacement we need in the instruction is 30 – 3 = 2D. • The target address calculation performed will be • (PC) + disp. • is set to 1 to indicate program-counter relative address, making the last 2 bytes of the instruction 202D.
2.2 Machine-dependent assembler (5)Thedifferencebetweenpcrelative addressingandbase relative addressingisthattheassemblerknowswhatthe contentsofthe program-counterwillbe atexecutiontimebutthebaseregisteris under thecontroloftheprogrammer.
2.2 Machine-dependent assembler • Example of program-counter relative assembly is the instruction: • 40 0017 J CLOOP 3F2FEC • The programmer must tell the assembler what the base register will contain during execution of the program so that the assembler can compute displacements.
2.2 Machine-dependent assembler The statement BASE LENGTH (line 13) informs the assembler that the base register will contain the address of LENGTH. (LDB #LENGTH) loads this values into the register during program execution. BASE and NOBASE are assembler directives, and produce no executable code. The programmer must provide instructions that load the proper value into the base register during execution. The target address calculation will not produce the correct operand address.
2.2 Machine-dependent assembler 160 104ESTCH BUFFER,X 57C003 The address of BUFFER is 0036.and are set to 1 in the assembled instruction. The difference between the assembly of the instructions on lines, LDA LENGTH is assembled with program-counter. STX LENGTH uses base relative displacement required for the statement on line 175, you will see that into the 12-bit displacement field. program-counter relative assembly first.
2.2 Machine-dependent assembler (6)Thedisplacementof pcrelativemodeisbetween -2048and+2047butthedisplacement ofbaserelativemode is between0and4095.ForSIC/XE assembler,itattemptpcrelativemode assemblyfirst.
2.2 Machine-dependent assembler Desirable to have more than one program at a time sharing the memory and other resources of the machine. It is not practical to plan program execution this closely. Situation the actual starting is not known until load time. Program (Fig.2.3), register A is to be loaded at address 2000 instead of address 1000. Address 102D will not contain the value that probably be part of some other user’s program.
2.2 Machine-dependent assembler Not know the actual location where the program, the assembler can identify for the loader those parts program that need modification. Modification is called a relocatable program. The program from Figs. 2.5 and 2.6. This program loaded beginning at address 0000. The program beginning at address 7420 (Fig. 2.7),would need to be changed to 4B108456, the new address of RDREC.
2.2 Machine-dependent assembler 0000 5000 0006 5006 (+JSB RDREC) (+JSB RDREC) 1036 6036 RDREC RDREC 1076 6076 (a) (b) 7420 7426 (+JSB RDREC) 8456 RDREC 8496 Fig 2.7 Examples of program relocation (c)
2.2 Machine-dependent assembler RDREC is always 1036 bytes past the starting address of the program. 1.Insert the address of RDREC relative to the start of the program.(to 0 for the assembly.) 2.Add the beginning address of the program to the address field in the JSUB instruction at load time.
2.2 Machine-dependent assembler (7)Thekindofsharingofthecommonmemory among programs is called multiprogramming. An object program that contains the information necessary to perform address modification is call a relocatable program. Ex. 15 CLOOP +JSUB RDREC M00000705 . 4B101036 (CLOOP +JSUB RDREC) . . B410 . . 0000 0006 5000 5006 (RDREC CLEAR X) 1036 6036 4B106036 4B101036 M00000705 0007 Fig2.7
2.2 Machine-dependent assembler (8) For the loader, of course, must also be a part of the object program. We can accomplish this with a Modification record having the following format: Modification record: Col. 1 M Col. 2-7Starting location of the addressfieldtobemodified,relativetothe beginning of the program. Col. 8-9Lengthoftheaddressfieldtobemodified in half-bytes. 15 CLOOP +JSUB RDREC
2.2 Machine-dependent assembler The length is stored in half-bytes, modified may not occupy an integral number of bytes.(20 bits, 5 half-bytes) The location of the byte containing the leftmost bits of the address field to be modified. M00000705 (5*4=20 bits ) This record specifies that the beginning address of the program is to begin at address 000007 and is 5 half-bytes in length. The first 12 bits (4B1), the last 20 bits (01036) to produce the correct operand address.
2.2 Machine-dependent assembler Modification is needed because the operand is specified using program-counter relative or base relative addressing. Thus no instruction modification is needed. The only such direct address are found in extended format (4-byte) instructions. Almost every instruction required modification. Show the complete object program corresponding to the source program of Fig. 2.5. There is one Modification record field that needs to be changed when the program is relocated.