joey miranda suba
Monday, March 14, 2011
Wednesday, March 9, 2011
structured programming
Structured programming (sometimes known as modular programming) is a subset of procedural programming that enforces a logical structure on the program being written to make it more efficient and easier to understand and modify. Certain languages such as Ada, Pascal, and dBASE are designed with features that encourage or enforce a logical program structure.
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.
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: The sequence construct is simply a list of one or more instructions that should be executed consecutively. There is no decision to be made, just instructions to be performed unconditionally. Such as The if-else structure tests a condition, then executes a construct of instructions based if the result of the condition was true. optionally, a different sequence construct of instructions may be executed in the event the result of the condition was false. That is, if. the condition is true, then one group of logic is processed, otherwise, a different group of logic may be processed. To set up an if-else construct, follow the these steps: The if-elseif-else construct also test a condition, then executes a sequence of instructions based on whether the result of the condition was true. Unlike the IF-ELSE construct which allows only one condition to be tested, the if -elseif -else construct allows for a list of conditions to be tested, executing the sequence of instructions provided when a condition tests true. -Optionally, a different sequence of instructions may be executed in the event that none of the tested conditions were true. That is, if the first condition is true, then one group of logic is processed, otherwise, the second condition is tested. If it is true, then its logic is processed, otherwise another condition may be tested. This may repeat for as long as you would like. In addition, an alternative group of logic may be specified in the event that ALL the previous conditions where not met. To set up an if-elseif-else structure, follow these steps:
It is often necessary to repeat a sequence of instructions. The do-while structure will repeat a sequence of instructions for as long as some defined condition is true. When condition is no longer true, execution continues at instruction after the do-while. That is, a group of repeated for as long as some condition remains true. The following guidelines for designing a do-while should be used:
Once pseudocode has been written, the writing of an assembler program merely becomes an act of translation. Let's first look at some pseudocode we would like to convert into assembler. This portion of pseudocode describes how to read an unknown number of cards, each card with an unknown number of values. It should print out each number, along with a message that says whether it was positive, negative, or zero. If no numbers where pulled, it should print an error message.
Our program now looks like the following:
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
Monday, February 28, 2011
programming practices
1. programming readability:
- use meaningful identifiers
- logic structure
- coding practice
- notes and comments
2. program documentation
- program listing
- program specification
- file layouts and record description
- operating instructions
- use meaningful identifiers
- logic structure
- coding practice
- notes and comments
2. program documentation
- program listing
- program specification
- file layouts and record description
- operating instructions
Tuesday, February 15, 2011
coverage of midterm exam
TOPICS
1. identifiers
2. assignment
3. expressions
4. control structures
5. web programming languages
we will meet first to discuss the remaining lesson and then exam
1. identifiers
2. assignment
3. expressions
4. control structures
5. web programming languages
we will meet first to discuss the remaining lesson and then exam
Monday, February 14, 2011
Monday, February 7, 2011
Subscribe to:
Posts (Atom)