900 likes | 1.11k Views
2. Introduction. LabDebuggingAttempt to debug yourself before taking to an instructor or to a TA (not to lab assistants).Those with flowchart, doc,
E N D
1. 1 Introduction to Assembler Department of Computer Science
Northern Illinois University
1999
2. 2 Introduction Lab
Debugging
Attempt to debug yourself before taking to an instructor or to a TA (not to lab assistants).
Those with flowchart, doc, & executed program might go to the head of the line when asking for help.
3. 3 Introduction Very rushed in first few weeks.
Do all reading by next class.
Get started on programs early.
Always bring your yellow card (little yellow booklet) to class.
Can even use in exams as long as nothing written on it.
Skim class notes good stuff in there.
Cheating Dont.
4. 4 Terminology (address book example) Field: A basic fact.
Like first name, last name, street address.
Composite field: a field made up of other fields.
Like address, which consists of street, city, state, and zip.
5. 5 Terminology Record: a specific set of fields.
Like all of the information stored for Joe Blow or Sue Smith.
File: a set of records.
Like the entire address book.
Files are also called data sets.
6. 6 Terminology Job Control Language (JCL): commands that tell the computer what to do with your program.
We give you the JCL you need; see class notes.
If the only thing that prints for your program is the header page, check your JCL.
7. 7 Terminology Hardware, Software
Operating system: the underlying computer program that serves as the traffic cop for the entire system, including hardware and other programs.
Examples: Windows, Unix, DOS, MVS, etc.
8. 8 Terminology Computer program/application program: a set of instructions, written according to precise rules, which the computer interprets and executes to perform a specific task.
Example: extracting all of the address book records for people that live in a given city.
Subroutine: A lower-level program that is called by higher-level programs.
A subcontractor program.
9. 9 Terminology Source program: the instructions that we write in Assembler, or COBOL, or Java, or any other programming language.
Compiler or Assembler: a program that translates source programs into object programs.
Object Program: the machine instructions that can be executed by the computer.
10. 10 Input, Processing, and Output Input: Incoming files.
Example: the address book and also the field qualifier/search field with the city name.
Processing: What the program does with the incoming files.
Example: Reading all the input records and extracting those with the city = the desired city.
11. 11 Input, Processing, and Output Output: New files produced by the program, including:
New data files on disk.
Printed reports.
Example: a report listing all of the people who live in the desired city.
12. 12 Input, Processing, and Output Input, processing, and output must be precisely defined.
Computers do what you tell them to do, not necessarily what you want them to do.
Computers are dumb; they are not mind readers.
So, we must be very careful about how we structure our programs.
13. 13 BAL: Basic Assembler Language Developed for the 360 class of IBM mainframes a very, very long time ago.
One of the most primitive programming languages.
The main use for BAL is for programming operating systems.
14. 14 Running a Program Two steps in running an assembler program:
1. The assembler assembles the source code into object code.
Errors at this stage are called syntax errors typos, spelling errors, formatting errors, etc.
15. 15 Running a Program 2. The object code is executed.
Errors at this stage are logic errors we didnt structure our program properly so that it executes properly.
An abend (abnormal termination) occurs when the program has such a serious error that it cant run to completion.
Example: attempting to divide by zero.
16. 16 Sample Program Look at page 34 in textbook, Program 2-1.
Well take a quick look at this just so that you see what an Assembler program looks like.
Later well look at this program in more depth.
17. Decimal address (offset) of each instruction/storage area
0 ADD2 CSECT
* THIS PROGRAM ADDS TWO NUMBERS FROM
* STORAGE TOGETHER.
0 L 1,16(,15) GET FIRST NUMBER
4 L 2,20(,15) GET SECOND NUMBER
8 AR 1,2 ADD TWO NUMBERS
10 ST 1,24(,15) STORE NUMBER
14 BCR B'1111',14 EXIT
16 DC F'4' FIRST NUMBER
20 DC F'6' SECOND NUMBER
24 DS F PLACE TO STORE SUM
28 END ADD2
18. 18 RR Instructions (AR, SR, LR) Finally, going to learn how to write a piece of code.
Several general types of instructions.
RR instructions mean register to register.
Format of stuff on slides:
r1, r2, etc., means some appropriate register.
R8, or just 8, means a specific register, in this case, register #8.
19. 19 Add Registers Instruction Format:
AR r1,r2 Add the 2nd register into the 1st register.
Example:
AR 8,6 Add R6 into R8.
20. 20 Subtract Registers Instruction Format:
SR r1,r1 Subtract 2nd register from 1st reg.
Example:
SR 3,5 Subtract R5 from R3.
21. 21 Load Register Instruction
22. 22 Load Register Instruction What if SR 9,9?
R9 = 0
Best way to initialize a register to zero.
What if AR 3,3?
Easiest way to multiply by two, as long as you dont need to keep the original value of the register.
23. 23 Errors on RR Instructions Possible errors on an RR instruction:
Fixed point overflow; number is too large to fit into the register.
Should abend, but doesnt usually do so in Assist (the assembler we use).
24. 24 Encoding/Decoding Object Code Of course, we would rather write symbolic instructions such as SR 4,7 than have to encode each instruction ourselves as machine code.
Still, assembler programmers need to understand how to encode/decode to machine instructions.
In assembler programming, you use this a lot for debugging.
25. 25 Encoding RR Instructions An RR instruction is stored in 16 bits = a byte = a halfword = 4 hex digits.
See yellow card, page 2.
h = some hex digit
h0h0hr1hr2
h0h0 = two digit operation (op) code.
See page 4 of yellow card.
h1 & h2 = 1st and 2nd registers
26. 26 Encoding RR Instructions Examples:
AR 8,6 ? 1A86
SR 3,5 ? 1B35
LR 7,13 ? 187D
27. 27 Decoding RR Instructions Work backwards from machine code.
See page 12 of yellow card for converting machine op code into source op code.
Examples:
1A2B ? AR 2,11
1B3C ? SR 3,12
1834 ? LR 3,4
28. 28 RX Instructions RX instructions involve a register and an address in storage (instead of a 2nd register).
Use Load to copy a fullword at D(X,B) into a register.
Use STore to copy a number in a register into a fullword at D(X,B).
The registers are automatically fullwords.
The D(X,B) addresses must resolve to a fullword boundary, or the program abends.
29. 29 Load Instruction Review: To resolve the D(X,B) address 12(5,15), add the following:
1210 = C16
R5 contains 0000001016
R15 contains 0000123416
0000125016 = address of fullword
A valid fullword address will always end in 0, 4, 8, or C: those are the hex numbers that are divisible by 4 and therefore on f.w. boundaries.
30. 30 Load Instruction
31. 31 Load Instruction Remember, the resolved address is not what is put into the register; the resolved address just points at the fullword number that will be put into the register.
32. 32 STore Instruction
33. 33 Errors on RX Instructions specification exception calculated address not on a full-word boundary; i.e., divisible by 4.
protection exception calculated address is outside the boundaries of the program.
addressing exception calculated address is too big to exist in memory at all.
Codes for these on p. 23 of yellow card.
Know these errors!
34. 34 Encoding RX Instructions 32 bits = a fullword = 8 hex digits.
On a halfword boundary.
h0h0hrhxhbhDhDhD
h0h0 = two digit op code.
hr = register that will hold (Load) or already does hold (STore) the result.
hx = index register.
hb = base register.
hDhDhD = displacement, now translated to hex.
35. 35 Encoding RX Instructions Examples:
A 8,12(3,15) ? 5A83F00C
S 3,4(4,5) ? 5B345004
L 7,8(2,3) ? 58723008
L 7,8(0,3) ? 58703008
36. 36 Decoding RX Instructions Examples:
5A284014 ? A 2,20(8,4)
5BA56010 ? S 10,16(5,6)
58329004 ? L 3,4(2,9)
37. 37 Typing Formats GET1 L 1,16(3,15) GET FIRST NUMBER
38. 38 Typing Formats Rules for assigning labels:
one to 8 characters
first character an A-Z (upper case), $, #, or @.
remaining characters can include the digits 0 through 9.
Examples of valid labels: MYPROG, MYPROG12, @MYPROG, $MYPROG, $1234567.
Labels are optional; use them when you have reason to use them.
39. 39 Typing Formats * THIS PROGRAM ADDS TWO NUMBERS FROM
* STORAGE TOGETHER.
Asterisk (*) in column 1 a comment, no code generated .
40. 40 Typing Formats Statements:
L 1,16(,15) GET FIRST NUMBER
41. 41 Non-executable Instructions to the Assembler They dont generate real code for the execution step.
Instead, they tell the assembler what to do during the assembly step.
42. 42 Non-executable Instructions to the Assembler CSECT must be used to indicated the beginning of the program.
Label (following the above rules).
CSECT in col 10.
Nothing else.
Example:
ADD2 CSECT
43. 43 Non-executable Instructions to the Assembler END instruction end of program.
END in col 10.
Label from CSECT as operand in col 16.
Nothing else.
Example:
END ADD2
44. 44 Data Constant Statement DC statement sets aside a storage area and assigns it an initial value.
COBOL equivalent: a Working Storage field with a VALUE IS clause.
45. 45 Data Constant Statement Format:
label DC mFn or mCLncharacter value
label in column 1.
Can use this label instead of a D(X,B) address in our programs (much easier).
DC in column 10.
46. 46 Data Constant Statement mF 'n in column 16. Example: 3F0
m is the repetition factor a decimal integer that tells the number of fullwords to set aside.
Default value is 1 if omitted.
F indicates that fullwords are reserved.
The F forces alignment on a fullword boundary.
'n' is the value to insert in each of those fullwords.
In decimal.
This would be the VALUE IS statement in COBOL.
or...
47. 47 Data Constant Statement mCLncharacters in column 16. Example: 3CL2HI
m is again the repetition factor.
CL indicates character data rather than fullwords.
Character data is not aligned on any boundaries.
n is the character data inserted in the field.
48. 48 Data Constant Statement Examples:
MULTIPLR DC F26
Produces 00000001A in a fullword in storage.
NUMBERS DC 2F1
Produces 00000001 00000001, two fullwords.
HEADER DC CL14STUDENT REPORT
Produces E2E3E4C4C5D5E340D9C5D7D6D9E3
See page 37, EBCDIC column ? hex column.
Hex 40 = space.
GREETING DC 2CL2HI
Produces C8C9C8C9
49. 49 Data Constant Statement DC statements can occur anywhere in program, but usually put at the end of a routine, just before the END statement.
50. 50 Data Storage Statement Setting up storage to hold values that will vary throughout the life of the program, with no initial values.
Format:
label DC mF or mCLn
label in col. 1.
DS in col. 10.
51. 51 Data Storage Statement mF
m repetition factor, same as DC
F again, reserves fullwords.
or
mCLn
m repetition factor, same as DC
CL again, reserves character bytes.
CL not on fullword boundaries.
n number of bytes for each repetition.
52. 52 Data Storage Statement Examples:
MULTIPLR DS F
Reserves one fullword, contents unspecified.
NUMBERS DS 2F
Reserves two fullwords, contents unspecified.
HEADER DS CL14
Reserves 14 bytes, contents unspecified.
GREETING DS 2CL2
Reserves 4 bytes, contents unspecified.
53. 53 Data Storage Statement LOOPTOP DS OH
Reserves 0 bytes, but assigns a label to this point in the program.
DS 0H is often embedded within the commands, just to assign a label.
54. 54 Data Storage Statement DCs and DSs grouped together at end of routine, usually just before the END statement.
Careful not to mix up DS/DC:
MULTIPLR DS F5
DS means the 5 is ignored; no initial value is assigned.
But no error message is issued, either.
Common error.
55. 55 Reserved Registers R15 always contains the absolute address of the beginning of the program when program begins execution .
So, R15 can be used as that ever important base register for all address calculations.
Which means you cannot use it for any other purpose, or you will wipe out your base register and no addresses will work.
56. 56 Reserved Registers R14 contains the absolute address of the routine to take control when your program terminates.
So, at the end of a program, you need to return control to where R14 is pointing.
Use R14 and the BCR statement to exit the program...
57. 57 Reserved Registers Format:
label BCR B'1111', 14
Example:
ENDPRG BCR B1111, 14
We will look at this statement more closely later; for now, just use it without understanding it.
58. 58 Reserved Registers Again, cannot use R14 or R15 for any other purposes.
Revisit Program 2-1 from textbook, this time for:
Use of R15, R14, and displacements.
Load, add, and store statements.
DC and DS statements.
59. Decimal address (offset) of each instruction/storage area
0 ADD2 CSECT
* THIS PROGRAM ADDS TWO NUMBERS FROM
* STORAGE TOGETHER.
0 L 1,16(,15) GET FIRST NUMBER
4 L 2,20(,15) GET SECOND NUMBER
8 AR 1,2 ADD TWO NUMBERS
10 ST 1,24(,15) STORE NUMBER
14 BCR B'1111',14 EXIT
16 DC F'4' FIRST NUMBER
20 DC F'6' SECOND NUMBER
24 DS F PLACE TO STORE SUM
28 END ADD2
60. 60 XDUMP Statement We need to display results of our actions somehow, like the sum calculated in our sample program.
For now, we are cheating will use XDUMP, a pseudo instruction (not real Assembler) provided by Assist.
61. 61 XDUMP Statement Format 1:
label XDUMP D(X,B), length
This dumps out memory starting at D(X,B), for the number of bytes specified by length (in decimal).
Data is dumped in hex format, with no formatting.
62. 62 XDUMP Statement Example:
XDUMP 124(,R15),100
Produces a dump of data starting at 124 bytes from the beginning of the program and continuing until it dumps 100 bytes.
XDUMP TABLE,400
Produces a dump starting at the beginning address of TABLE and continuing until it dumps 400 bytes.
63. 63 XDUMP Statement Format 2:
label XDUMP
Dumps out the contents of all registers.
Example:
DUMPIT2 XDUMP
64. 64 XDUMP Statement XDUMP of storage will always print a full 32-byte line, even if you request only 4 bytes.
So, you often get extra bytes at the beginning and end of the dump.
You will use XDUMPs to display the results of your early programs, just until we teach you better methods.
65. 65 XDUMP Statement In real life, XDUMP used only for debugging to dump registers and storage to see why your program isnt working.
Use XDUMPs liberally while debugging.
Precede each XDUMP with an
XPRINT label
so that when the dump prints, you will know where in your code the dump was requested.
66. 66 XDUMP Statement If your program abends, you will get both types of dumps automatically.
67. 67 Implicit Addressing Explicit addressing: giving an exact location.
Example: 312 Annie Glidden.
Relative addressing: D(X,B)
Example: "two doors west of Mabel's house
Using R15 as the base address (Mabels house) and adding the displacement (2 doors west) from the beginning of the program.
68. 68 Implicit Addressing Implicit addressing: giving an area in storage a name, then using the name.
Example: Lowden Hall
Implicit addresses are resolved to relative addresses by the computer.
Since those relative addresses rely on R15 as the base register, R15 must be correct for this to work.
69. 69 Implicit Addressing Can give any instruction or storage area a label, then refer to it by that label.
Eliminates much of the calculations needed for addresses.
To make implicit addressing work, ...
70. 70 Implicit Addressing USING statement Tells the assembler what register will point to the beginning of the program, so it can be used to calculate addresses.
Format:
USING label,r
label is the label attached to the CSECT statement.
r is the base register, always R15 for now.
71. 71 Implicit Addressing Example:
USING ADD2,15
tells the assembler that the program starts at ADD2, and that
R15 is the base register.
We call this establishing addressability.
Again, you must have a USING statement if you want to use implicit addressing (labels).
72. 72 Implicit Addressing Example of implicit addressing:
...
ST 1,NUM1 Store R1 in NUM1.
...
NUM1 DS F
A whole lot easier than calculating the displacement, huh?
73. 73 New Version of ADD2 So lets look at a new version of our program, new stuff highlighted
74. ADD2 CSECT
* THIS PROGRAM ADDS TWO NUMBERS FROM
* STORAGE TOGETHER.
USING ADD2,15
L 1,NUM1 GET FIRST NUMBER
L 2,NUM2 GET SECOND NUMBER
AR 1,2 ADD TWO NUMBERS
ST 1,SUM STORE NUMBER
XDUMP SUM,4 DUMP OUT THE ANSWER
BCR B'1111',14 EXIT
NUM1 DC F'4' FIRST NUMBER
NUM2 DC F'6' SECOND NUMBER
SUM DS F PLACE TO STORE SUM
END ADD2
75. 75 New Version of ADD2 Repeat: the USING statement is what allows us to use implicit addresses.
76. 76 Another Wrinkle on Implicit Addressing Can combine implicit addressing with an index register:
label(r)
Where r is an index register giving the displacement from the beginning of the field.
Used often in table processing.
77. 77 Another Wrinkle on Implicit Addressing Example 1:
TABLE DS 9F
TABLE(5) would add the contents of R5 to the address of TABLE.
COBOL programmers: this does not get the fifth entry.
As is often the case in assembler, we are a step removed from what we really want to do.
78. 78 Another Wrinkleon Implicit Addressing Example 2:
TABLE + n
adds in n as a displacement of sorts.
TABLE +10
Would add 10 bytes to the beginning address of TABLE.
79. 79 Miscellaneous Stuff As a rule, we code everything (like displacements, registers, values in DC statements) in decimal, not hex.
The assembler translates everything to hex.
We worry about hex only when debugging and checking dumps.
80. 80 Miscellaneous Stuff In all assembler instructions, operands in col. 16 (register numbers, addresses, labels, etc.) are not actually the data, just indications of where to find the real data.
Think of the operands the labels on buckets, and the real data is inside those buckets.
81. 81 Compare Registers Instruction CR r1, r2 Compare two registers.
RR instruction
Sets Condition Code to:
0 when the two registers are equal
1 when r1 is smaller, and
2 when r2 is smaller
Later we will tell you how to use the CC settings.
82. 82 Load Complement Register Instruction LCR r1,r2 Load complement.
Reverses the sign of the number; i.e., puts the twos complement of r2 in r1.
Sets Condition Code to:
0 when r1 is zero.
1 when r1 is negative.
2 when r1 is positive.
83. 83 Load Negative Register, Load Positive Register Instructions LNR r1,r2 Load negative register; i.e., always a negative result in r1.
LPR r1,r2 Load positive register; i.e., always a positive result in r1.
r2 is unchanged.
Sets Condition Code to:
0 when r1 is zero.
1 when r1 is negative.
2 when r1 is positive.
84. 84 Load and Test Register LTR r1,r2 Load and test register.
Loads number and tests it to see if zero, negative, or positive.
New value in r1, r2 is unchanged.
Same as LR, but sets the condition code:
0 when r1 is zero.
1 when negative.
2 when r1 is positive.
85. 85 Load and Test Register If you need to test a register, but dont want to copy it to another register:
LTR 12,12
Value in R12 unchanged, but sets CC to tell us if number is zero, negative, or positive.
86. 86 More RX Instructions Many RR instructions (register to register) have RX (register to address) counterparts.
87. 87 Add and Subtract Instructions A r,D(X,B) Add contents of the fullword to the register.
A 3,NUM1 Add contents of NUM1 to R3.
S r, D(X,B) Subtract contents of the fullword from the register.
S 7,NUM1 Subtract NUM1 from R7.
88. 88 Compare Instruction C r, D(X,B) Compare the register to the fullword in storage.
C 9, NUM1 Compare R9 with NUM1.
Same condition code settings as CR.
89. 89 RX Instruction Errors Same as already stated for previous RX instructions:
specification exception address not on a full-word boundary; i.e., divisible by 4.
protection error calculated address is outside the boundaries of the program.
addressing exception calculated address is too big to exist in memory at all.
Also overflow for A and S (arithmetic).
90. 90 Program Execution The absolute address of first instruction to be executed is inserted into the PSW (program status word).
The machine retrieves the instruction at which the PSW points.
The machine updates the PSW to point to the next instruction.
The machine executes the instruction that was retrieved.