Structured programming frequently employs a top-down design model, in which developers map out the overall program structure into separate subsections. A defined function or set of similar functions is coded in a separate module or submodule, which means that code can be loaded into memory more efficiently and that modules can be reused in other programs. After a module has been tested individually, it is then integrated with other modules into the overall program structure.
Program flow follows a simple hierarchical model that employs looping constructs such as "for," "repeat," and "while." Use of the "Go To" statement is discouraged.
Structured Pseudocode
The structuring of a program's logic is a fairly recent concept which has been gaining popularity and respect. Structured programming provides the programmer logical constructs from which to build efficient programs. It facilitates simplified logic, easier debugging, and better maintenance. Structured programming is best demonstrated through the use of pseudocode. Pseudocode is an English like language that allows the description of logic to be written, free from the syntax of a formal computer language. Therefore, a particular set of pseudocode becomes portable. That is, it may be shared with different programmers, each of which may work with a different language, and it will be readily understood. Actual computer programming simply becomes the translation of pseudocode into a computer language. The following are four basic control constructs that we will use in pseudocode to give our programs good structure: - sequence (straight-line)
- if-else
- if-elseif-else
- do-while
SEOUENCE CONSTRUCT
X=5.2
Y=X*3
PRINT X,Y
PULL UP TO GAS STATION
REMOVE NOZZLE ON GAS PUMP
INSERT NOZZLE IN GAS TANK
FILL TANK WITH GAS
REPLACE NOZZLE ON GAS PUMP
PAY FOR GAS
LEAVE GAS STATIONA sequence of instructions is always embedded in the three other constructs. IF-ELSE CONSTRUCT
- Place an 'IF' command, followed by the condition to be tested on the top line.
- Indent, and place the sequence of instructions that should be executed if the result of the condition is true.
- If there is a separate sequence of instructions to be executed if the result is false, place an 'ELSE' command in the same column as the ' IF', then on the next line and indented, place the sequence of instructions.
- Place an 'ENDIF' command in the same column as the 'IF' command to signal the end of the if-else structure.
IF X = 0
Y=O
PRINT 'Division by zero avoided.'
ELSE
Y=100/X
ENDIF
IF Y > 0 AND Y < 200
PRINT Y
ENDIF
IF CAR LOW ON GAS
PULL INTO GAS STATION
REMOVE NOZZLE ON GAS PUMP
INSERT NOZZLE IN GAS TANK
FILL TANK WITH GAS
REPLACE NOZZLE ON GAS PUMP
PAY FOR GAS
LEAVE GAS STATION
ELSE
DRIVE PAST GAS STATION
ENDIF
IF WINDSHIELD IS DIRTY
CLEAN WINDSHIELD
ENDIF IF-ELSEIF-ELSE CONSTRUCT
- Place an 'IF' command, followed by the condition to be tested on the top line.
- Indent, and place the sequence of instructions that should be executed if the result of the condition is true.
- Place an 'ELSEIF' command in the same column as the 'IF', followed by the next condition to be tested. Then on the next line and indented, place the sequence of instructions to be executed if this condition resulted in true. Repeat this step for as many choices you have to be made.
- If there is a separate sequence of instructions to be executed if ALL the previous conditions resulted is false, place an 'ELSE, command in the same column as the 'IF', then on the next line and indented, place the sequence of instructions.
- Place an 'ENDIF' command in the same column as the 'IF' command to signal the end of the if-elseif-else structure.
READ TRANSACTION-CODE
IF TRANSACTION-CODE = 'A"
ADD THE NEW STUDENT RECORD
PRINT THE NEW STUDENT RECORD
ELSEIF TRANSACTION-CODE = 'D'
DELETE THE CURRENT STUDENT
PRINT A DELETE MESSAGE
ELSEIF TRANSACTION-CODE = 'E'
EDIT THE CURRENT STUDENT
PRINT THE CHANGES
ELSE
ENDIF PRINT 'INVALID TRANSACTION CODE,
IF HOME STEREO DOES NOT WORK
IF NO POWER
CHECK TO MAKE SURE UNIT IS PLUGGED IN
CHECK TO MAKE SURE POWER OUTLET WORKS
ELSEIF NO SOUND
CHECK SPEAKERS
ELSE
CALL MANUFACTURER
ENDIF
ENDIF DO-WHILE CONSTRUCT
- Place a 'DO' command, followed by the condition tested on the top line.
- Indent, and place the sequence of instructions should be executed if the result of the cc true.
- Place an ENDDO command in the same column a command to signal the end of the do-while
READ RECORD
DO WHILE (NOT END-OF-FILE)
PROCESS RECORD
READ NEXT RECORD
ENDDO
DO WHILE DIRTY DISHES ON COUNTER
GRAB DISH
WASH DISH
RINSE DISH
DRY DISH
PUT DISH AWAY IN CUPBOARD
ENDDO CONVERTING PSEUDOCODE INTO ASSEMBLER
1 ROUTINE:CHECKNUM
2 COUNT = 0
3 READ FIRST CARD
4 DO WHILE (NOT END-OF-FILE)
5 ATTEMPT TO GET NUMBER FROM CARD
6 DO WHILE (SUCCESSFUL)
7 COUNT = COUNT + 1
8 PUT NUMBER ON PRINTLINE
9 IF NUMBER = 0
10 PUT 'ZERO' ON PRINTLINE
11 ELSEIF NUMBER < 0
12 PUT 'NEGATIVE' ON PRINTLINE
13 ELSE
14 PUT 'POSITIVE' ON PRINTLINE
is ENDIF
16 PRINT PRINTLINE
17 ATTEMPT TO GET NEXT NUMBER FROM CARD
18 ENDDO
19 READ NEXT CARD
20 ENDDO
21 IF COUNT = 0
22 PRINT 'NO NUMBERS FOUND'
23 ENDIF
24 RETURNNote that the line numbers to the left are there only for reference purposes. The first thing that we should do is to label each do-while, if-else, and if-elseif-else construct. The label names we chose should conform to the assembler language standards of being up to eight characters in length, and starting with a letter from the alphabet. See the ASSIST text book for more information about assembler label names. A good practice is to chose labels that indicate the structure of the logic, not the logic's function. The logic will contain documentation to indicate its function. A simple method is to take the first character of the routines name and concatenate it with the pseudocode keyword command (such as Do, IF, ET IF, ELSE, ENDIF, ENDDO.) Keep a mental counter for each type of do-while, if- else, and if-elseif-else construct within the current routine.* You can use this to distinguish each embedded structure from the next, as the assembler will require each label to be unique. The best illustration of this is an example. 1 ROUTINE:CHECKNUM
2 COUNT = 0
3 READ FIRST CARD
4 CDO1 DO WHILE (NOT END-OF-FILE)
5 ATTEMPT TO GET NUMBER FROM CARD
6 CD02 DO WHILE (SUCCESSFUL)
7 COUNT = COUNT + 1
8 PUT NUMBER ON PRINTLINE
9 CIF1 IF NUMBER = 0
10 PUT 'ZERO' ON PRINTLINE
11 CELSE1A ELSEIF NUMBER < 0
12 PUT 'NEGATIVE' ON PRINTLINE
13 CELSE1 ELSE
14 PUT 'POSITIVE' ON PRINTLINE
15 CENDIF1 ENDIF
16 PRINT PRINTLINE
17 ATTEMPT TO GET NEXT NUMBER FROM CARD
18 CENDD02 ENDDO
19 READ NEXT CARD
20 CENDD01 ENDDO
21 CIF2 IF COUNT = 0
22 PRINT 'NO NUMBERS FOUND'
23 CENDIF2 ENDIF
24 RETURNOur sample pseudocode now contains assembler labels for each pseudocode command. Now we need to define the process of converting pseudocode into assembler code. | Statement Type | Action |
|---|---|
| ROUTINE | Code the routine name as the CSECT. Establish addressability |
| no label | Encode the one pseudocode instruction into as many assembler instructions as it takes to complete. |
| DO | Place the label in the left-hand column. Set the condition code if necessary. Branch to the matching ENDDO if the condition is not met. |
| IF | Place the label in the left-hand column. Set the condition code if necessary. Branch to the next label belonging within the IF structure (i.e. ELSEIF, ELSE, or ENDIF) if the condition is not met. |
| ELSEIF | Branch unconditionally to the ENDIF. Place the label in the left-hand column. Set the condition code if necessary. Branch to the next label belonging within the current IF structure (i.e., ELSEIF, ELSE, or ENDIF) when the condition is not met. |
| ELSE | Branch unconditionally to the ENDIF. Place the label in the left-hand column and leave a place holder (DS 0H). |
| ENDIF | Place the label in the left-hand column and leave a place holder (DS 0H). |
| ENDDO | Branch unconditionally to the corresponding DO label this ENDDO closes. Place the label in the left-hand column and leave a place holder (DS 0H). |
| RETURN | Code the assembler instructions to return to the caller. Code an LTORG, then any storage this routine will need. |
1 CHECKNUM CSECT
USING CHECKNUM,R15
2 SR R3,R3
3 XREAD CARD,80
4 CDO1 BNZ CENDD01
5 XDECI R2,CARD
6 CD02 BO CENDD02
7 LA R3,1(R3)
8 XDECO R2,OUTNUM
9 CIF1 BNZ CELSE1A
10 MVC OUTMSG(20),=CL20'ZERO'
11 B CENDIF1
CELSE1A BNM CELSE1
12 MVC OUTMSG(20),=CL20'NEGATIVE'
13 B CENDIF1
CELSE1 DS 0H
14 MVC OUTMSG(20),=CL20'POSITIVE'
15 CENDIF1 DS 0H
16 XPRNT PLINE,56
17 XDECI R2,0R1)
18 B CD02
CENDD02 DS 0H
19 XREAD CARD,80
20 B CDO1
CENDD01 DS 0H
21 CIF2 LTR R3,R3
BNZ CENDIF2
22 XPRNT =CL20' NO NUMBERS FOUND',20
23 CENDIF2 DS 0H
24 BR R14
LTORG
CARD DC CL80' '
DC C'*'
PLINE DC CL161 NUMBER READ WAS'
OUTNUM DC CL12' '
DC CL8' AND IS '
OUTMSG DC CL20' '
END CHECKNUM
czelinasese.blogspot.com
ReplyDeletekristinepingol.blogspot.com
ReplyDeletedychitanabi.blogspot.com
ReplyDeletenozaca.blogspot.com
ReplyDeletefhat04.blogspot.com
ReplyDeleteconsantos.blogspot.com
ReplyDeletehttp://adrianecarlo.blogspot.com/
ReplyDeletehttp://ggtaruc.blogspot.com
ReplyDeleteainehaldos.blogspot.com
ReplyDeleteabbymungcal.blogspot.com
ReplyDeletejisip.blogspot.com
ReplyDeleteiceiceyelo.blogspot.com
ReplyDeletemyccasamson.blogspot.com
ReplyDeletepaolocanlas.blogspot.com
ReplyDeletezhellaxamana.blogspot.com
ReplyDeleteedrichmonddelacruz.blogspot.com
ReplyDeletejoelewissantos.blogspot.com
ReplyDeletejhayne050792.blogspot.com
ReplyDeletehttp://selectionsequence.blogspot.com/
ReplyDeletehttp://ryantiongson.blogspot.com/2011/03/sequence-system-source-code-output.html
ReplyDeletehttp://sequenceandselection.blogspot.com/2011/03/sequence-and-selection.html
ReplyDelete