Program Design Examples
E N D
Presentation Transcript
Program Design Examples Lecture 3 - Instruction Set - Al
Lecture Overview • Have seen the initial part of • Top down design • Modular design • Parameter Passing • Stack and Local Variables • Today • Structured Programming continued • HLL Structures to assembler • Example Lecture 3 - Instruction Set - Al
Assembler for HLL Expressions • HLL: IF (cond L) THEN Action_S • Assume data, result of evaluating condition L, to be tested in D0. It has a value of either 1 (true) or 0 (false). TST.B D0 test low byte DO BEQ.S EXIT action_S EXIT • The BEQ.S is a short 8-bit branch where the target address displacement is <= what can be specified by 8-bits Lecture 3 - Instruction Set - Al
If the data L to be tested in reg • If the data L to be tested is in register D0 and the test is L = 0 with action S if L=0 • Could do • CMPI.W #0, D0 • BNE around • action S • around following_instructions • Note: This does explain how to do a test with the results of the test in D0. That would take a couple of instructions to do. Ref page 66. Lecture 3 - Instruction Set - Al
Assembler for HLL Expressions 2 • Here D0 has result of test of condition • HLL: IF (cond L) THEN S1 ELSE S2 • TST.B D0 • BEQ.S ELSE • S1_actions • BRA.S EXIT • ELSE S2_actions • … • EXIT Lecture 3 - Instruction Set - Al
Assembler for HLL Expressions 3 • HLL: FOR I = N1 TO N2 DO S • MOVE.B #N1,D1 d1 is loop cntr • NEXT S_actions • ADDQ.B #1,D1 inc loop cntr • CMP.B #N2+1,D1 test • BNE NEXT • EXIT Lecture 3 - Instruction Set - Al
Assembler for HLL Expressions 4 • HLL: WHILE L DO S • REPEAT TST.B DO • BEQ.S EXIT if 0 quit • S_actions • within actions will perform • a compare that leaves • the TRUE(1) or FALSE(0) • in D0 • BRA REPEAT • EXIT • NOTE: A while loop may or may not execute once. Why TST is at start. Lecture 3 - Instruction Set - Al
Assembler for HLL Expressions 5 • HLL: REPEAT S UNTIL L • NEXT S_actions • --and code that puts result of ‘L’ in D0 as T/F (F=0) • TST.B D0 • BEQ NEXT • EXIT Lecture 3 - Instruction Set - Al
Assembler for HLL Expressions 6 • HLL: FOR I=N DOWNTO -1 DO S • MOVE.W #n,D1 • NEXT S_actions • DBRA D1,NEXT • Decrement D1 and loop if not -1 Lecture 3 - Instruction Set - Al
Testability • In the real world $$$ come into play • Could test to degree that errors never occur • Test to degree that is rational • Structured code is more testable than alternatives • Modules are units to be tested • Interaction of modules is tested Lecture 3 - Instruction Set - Al
Recoverabiltiy • Also called exception handling • What does the system do with erroneous data • What does system do in response to certain classes or errors. • Decision on what action to take when these conditions occur, such as divide by 0. • A poorly designed plan may be far worse than no plan at all. Lecture 3 - Instruction Set - Al
Pseudocode (PDL) • PDL is a way to write down an algorithm • A compromise between HLL and assembler • Facilitates the production of reliable code • Much like modern HLLs Lecture 3 - Instruction Set - Al
Program Specs • D0 used for character input and output – low order byte of D0 • D1 contains code of 0,1, or 2 • 0 – clear the buffer and reset pointers • 1 – place character in D0 into buffer • 2 – remove character from buffer to D0 • Buffer starts at $01 0000 and is 1,024 bytes Lecture 3 - Instruction Set - Al
Program Specs 2 • Scratch storage may be placed after the end of the buffer – up to 32 bytes • When buffer is full oldest data is overwritten – bit 31 of D0 is set to indicate overflow • Underflow action – D0.B set to 0 – msb set • No other register modified Lecture 3 - Instruction Set - Al
Circular Buffer concept Lecture 3 - Instruction Set - Al
PDL • Level 1 • Module: Circular_buffer • Save working registers • Select one of: • Initialize System • Input a character • Output a character • Restore working registers • End Circular_buffer Lecture 3 - Instruction Set - Al
PDL • Level 2 • Module: Circular_buffer • Save working registers • IF [D1]=0 THEN Initialize END IF • IF [D1]=1 THEN Input_char END IF • IF [D1]=2 THEN Output_char END IF • End Circular_buffer Lecture 3 - Instruction Set - Al
PDL – level 2 continued • Initialize • Count=0 In_pointer := Start Out_pointer := Start End Initialize Lecture 3 - Instruction Set - Al
PDL – level 2 cont. • Input_character • Store new character Deal with any overflow End Input_character Routine is still a level too high to encode Lecture 3 - Instruction Set - Al
PDL – level 3 • Input_character • Store new character at in_pointer • In_pointer := In_pointer + 1 • If In_pointer>End THEN In_pointer:=Start endif • If Count < Max THEN Count:=Count+1 • ELSE • BEGIN • Set overflow flag ; • Out_pointer := Out_pointer + 1) Lecture 3 - Instruction Set - Al
continued • IF Out_pointer>End Then Out_pointer:=Start • END IF • END • END IF • End Input_character • This can then be coded in assembler or even HLL by translating HLL or pseudocode to assembler. Lecture 3 - Instruction Set - Al