Download presentation
Presentation is loading. Please wait.
Published byProsper Holmes Modified over 9 years ago
1
CIS 020 Assembly Programming Chapter 07 - Decimal Arithmetic Operations © John Urrutia 2012, All Rights Reserved.5/27/20121
2
Decimal Numbers Zoned-Decimal (printable character) format, 10 Bytes, can’t be used for calculations. To be used in calculations they must be converted into the Packed-Decimal (non-printable) format, 6 Bytes. Packing strips the zone nibble for each byte and shifts the decimal nibble to the left. © John Urrutia 2012, All Rights Reserved.25/27/2012 Print Character0123456789 Hexadecimal ValueF0F1F2F3F4F5F6F7F8F9 Numeric Value0123456789 Hexadecimal Value 00123456789F
3
Decimal Numbers Unless otherwise designated Zone-Decimal numbers are considered positive values. The sign of a number is stored in the rightmost byte For Zone-Decimal it is in the Zone portion of the byte For Packed-Decimal it is in the Digit portion of the byte © John Urrutia 2012, All Rights Reserved.35/27/2012
4
Decimal Numbers Unless otherwise designated Zone-Decimal numbers are considered positive values. Packed-Decimal format carries the sign of the number in the rightmost nibble The sign is base on the following values: © John Urrutia 2012, All Rights Reserved.45/27/2012 Numeric Value0123456789 Hexadecimal Value 00123456789F PositiveNegative 1010 – A 1100 – C 1110 – E 1111 – F 1011 – B 1101 - D Preferred Values
5
Convert from ZD to PD format PACK instruction converts Zone-Decimal numbers to Packed-Decimal numbers, and is storage to storage. Syntax of Instruction Op Code – PACK Operand 1 – label or address Operand 2 – label or address Instruction processes right to left © John Urrutia 2012, All Rights Reserved.55/27/2012
6
Convert from ZD to PD format Operand 1 determines how many bytes to process The Maximum number of bytes to pack is 16 which limits the length of Operand 2 to 16 bytes The length needed for Operand 1 is determined by this formula: (length of operand 2/2) + 1 The Assembler will not warn you if you make a mistake in this calculation © John Urrutia 2012, All Rights Reserved.65/27/2012
7
PACK examples © John Urrutia 2012, All Rights Reserved.75/27/2012 Normal – 5 bytes pack exactly into 3 bytes X'51234F'
8
PACK examples © John Urrutia 2012, All Rights Reserved.85/27/2012 Operand 1 short – 5 bytes truncate into 2 bytes
9
PACK examples © John Urrutia 2012, All Rights Reserved.95/27/2012 Operand 2 short – 3 bytes pad left into 3 bytes
10
PACK examples © John Urrutia 2012, All Rights Reserved.105/27/2012 Self Packed
11
PACK examples © John Urrutia 2012, All Rights Reserved.115/27/2012 Maximum data packed 45F ’
12
Relative Addressing Just like the previous instructions we can modify the length and offset of labels for PACK instructions in the same way. Each operand can be modified © John Urrutia 2012, All Rights Reserved.125/27/2012
13
PACK Constants Use the DC assembler directive to define all constant values. The data type identifier will dictate how much storage will be allocated to this constant. Sign is allowed but decimal point is not. You are responsible for knowing where the decimal point goes! © John Urrutia 2012, All Rights Reserved.135/27/2012
14
The Decimal Instructions Order of operation When you evaluate a numeric expression the order of operations govern the outcome. PEMDAS – mnemonic for the priority. Parenthesis – from innermost to outermost Exponentiation – Multiplication – Division – Addition – Subtraction – © John Urrutia 2012, All Rights Reserved.145/27/2012 Processed left to right
15
The Decimal Instructions AP, SP – All are storage-to-storage instructions just like PACK Sets the condition code, branch Mask 01 – condition code = 3Overflow / Underflow 02 - condition code = 2Plus / Positive 04 - condition code = 1Minus / Negative 08 - condition code = 0Zero 14 – not condition code = 3No Overflow / Underflow 13 – not condition code = 2Not Plus / Positive 11 – not condition code = 1Not Minus / Negative 07 – not condition code = 0Not Zero © John Urrutia 2012, All Rights Reserved.155/27/2012
16
AP – add packed Syntax of Instruction Op Code – AP Operand 1 –first addend (before execution) and sum (after execution) Operand 2 – second addend Both Operands must be in packed-decimal format Operand 1 should be one byte larger than the size of Operand 2 to prevent overflow conditions. All packed-decimal instructions set the sign to X’C’ – Positive or X’D’ – Negative © John Urrutia 2012, All Rights Reserved.165/27/2012
17
AP – add packed Example: CC=2 Plus value © John Urrutia 2012, All Rights Reserved.175/27/2012
18
AP – add packed Example: Data Exception Occurs (S0C7) © John Urrutia 2012, All Rights Reserved.185/27/2012
19
AP – add packed to multiply Example: CC=2 Plus value © John Urrutia 2012, All Rights Reserved.195/27/2012
20
Convert from PD to ZD format UNPK (unpack) instruction converts Packed-Decimal numbers to Zone-Decimal numbers, and is storage to storage. Syntax of Instruction Op Code – UNPK Operand 1 – label or address Operand 2 – label or address Instruction processes right to left © John Urrutia 2012, All Rights Reserved.205/27/2012
21
Convert from PD to ZD format Operand 1 determines how many bytes to process The Maximum number of bytes to unpack is 16 and so is the length of Operand 2 The length needed for Operand 1 is determined by this formula but can never be longer 16 bytes: (length of operand 2)*2 – 1 The Assembler will give a syntax error if the length of either operand is greater than 16 bytes © John Urrutia 2012, All Rights Reserved.215/27/2012
22
Convert from PD to ZD format Mixed length Operands cause the following: If Operand 1 can contain more digits than Operand 2, Operand 1 is padded to the left with zeros – X’F0’ If Operand 1 can’t contain all the digits in Operand 2, Operand 1 High-order values are truncated. © John Urrutia 2012, All Rights Reserved.225/27/2012
23
UNPK examples © John Urrutia 2012, All Rights Reserved.235/27/2012 Normal – 3 bytes unpack exactly into 5 bytes
24
UNPK examples Operand 1 larger – 2 bytes unpack into 5 bytes High-order bytes padded with zeros © John Urrutia 2012, All Rights Reserved.245/27/2012
25
UNPK examples Operand 2 larger – 3 bytes unpack into 3 bytes High-order byte values (1 & 2) truncated © John Urrutia 2012, All Rights Reserved.255/27/2012
26
UNPK examples Maximum Operand 1 – 16 Bytes © John Urrutia 2012, All Rights Reserved.265/27/2012
27
Securing the Sign ± The last digit of an unpacked number contain the sign of the number in the Zone portion Any arithmetic operation changes the sign to the preferred ‘C’ or ‘D’ these zone values are NOT numbers © John Urrutia 2012, All Rights Reserved.275/27/2012 HexCharacter X’C0’{ X’C1’A X’C2’B X’C3’C X’C4’D X’C5’E X’C6’F X’C7’G X’C8’H X’C9’I HexCharacter X’D0’} X’D1’J X’D2’K X’D3’L X’D4’M X’D5’N X’D6’O X’D7’P X’D8’Q X’D9’R
28
Securing the Sign ± We must set the zone to F OI – to the rescue Or Immediate is an instruction that allows us to modify any of the bits in a byte. It is used to make sure the last byte of an unpacked field will print as a number. © John Urrutia 2012, All Rights Reserved.285/27/2012
29
OI – or immediate Syntax of Instruction Op Code – OI Operand 1 –Label or address of byte to change Operand 2 – Mask of bits to turn-on The Mask of the instruction can be any byte of data and all of the on bits (1) will be reflected in the first operand. When securing the sign the address of the last byte of the field is designated. © John Urrutia 2012, All Rights Reserved.295/27/2012
30
OI – or immediate The Mask value B’00000000’ each digit represents a bit that will be turned-on after execution. The byte identified by operand 1 is modified by the mask. © John Urrutia 2012, All Rights Reserved.305/27/2012 Byte Operand X’C7’ B’11000111’ Mask Operand X’F0’ B’11110000’ Result Byte Operand X’F7’ B’11110111’ Each bit is turned on left to right
31
OI example Correct character for output © John Urrutia 2012, All Rights Reserved.315/27/2012
32
Countdown to the end We have learned enough at this point to be able to create a count controlled loop structure which we can use to simulate the factorial function in mathematics. Using just the instructions we have learned create a program that will : Solve factorial problems for 0 through 18. or Identify the smallest 16 digit Fibonacci number © John Urrutia 2012, All Rights Reserved.325/27/2012
33
Factorial and Fibonacci Factorial In mathematics, the factorial of a non-negative integer n, is the product of all positive integers less than or equal to n. ea. 5 factorial is equal to 120 = 5 * 4 * 3 * 2 * 1 ea. 6 factorial is equal to 720 = 6 * 5 * 4 * 3 * 2 * 1 © John Urrutia 2012, All Rights Reserved.335/27/2012
34
Factorial and Fibonacci Fibonacci In mathematical terms, the sequence F n of Fibonacci numbers is defined by the recurrence relation F n = F n-1 + F n-2 with seed values of F 0 = 0 and F 1 = 1 ea. F 2 = 1, F 3 = 2, F 4 = 3, F 5 = 5, F 6 = 8, F 7 = 13 The smallest 2 digit Fibonacci number is 13. © John Urrutia 2012, All Rights Reserved.345/27/2012
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.