CIS 020 Assembly Programming Chapter 07 - Decimal Arithmetic Operations © John Urrutia 2012, All...

34
CIS 020 Assembly Programming Chapter 07 - Decimal Arithmetic Operations © John Urrutia 2012, All Rights Reserved. 5/27/2012 1

Transcript of CIS 020 Assembly Programming Chapter 07 - Decimal Arithmetic Operations © John Urrutia 2012, All...

PowerPoint Presentation

CIS 020 Assembly ProgrammingChapter 07 - Decimal Arithmetic Operations John Urrutia 2012, All Rights Reserved.5/27/20121Decimal NumbersZoned-Decimal (printable character) format , 10 Bytes, cant 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/2012Print Character0123456789Hexadecimal ValueF0F1F2F3F4F5F6F7F8F9Numeric Value0123456789Hexadecimal Value00123456789FDecimal NumbersUnless otherwise designated Zone-Decimal numbers are considered positive values. The sign of a number is stored in the rightmost byteFor Zone-Decimal it is in the Zone portion of the byteFor Packed-Decimal it is in the Digit portion of the byte

John Urrutia 2012, All Rights Reserved.35/27/2012Decimal NumbersUnless 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/2012Numeric Value0123456789Hexadecimal Value00123456789FPositiveNegative1010 A 1100 C 1110 E 1111 F 1011 B 1101 - DPreferred ValuesConvert from ZD to PD formatPACK instruction converts Zone-Decimal numbers to Packed-Decimal numbers, and is storage to storage.Syntax of InstructionOp Code PACKOperand 1 label or addressOperand 2 label or addressInstruction processes right to left John Urrutia 2012, All Rights Reserved.55/27/2012Convert from ZD to PD formatOperand 1 determines how many bytes to processThe Maximum number of bytes to pack is 16 which limits the length of Operand 2 to 16 bytesThe 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/2012PACK examples John Urrutia 2012, All Rights Reserved.75/27/2012Normal 5 bytes pack exactly into 3 bytes

X'51234F'PACK examples John Urrutia 2012, All Rights Reserved.85/27/2012Operand 1 short 5 bytes truncate into 2 bytes

PACK examples John Urrutia 2012, All Rights Reserved.95/27/2012Operand 2 short 3 bytes pad left into 3 bytes

PACK examples John Urrutia 2012, All Rights Reserved.105/27/2012Self Packed

PACK examples John Urrutia 2012, All Rights Reserved.115/27/2012Maximum data packed

45FRelative AddressingJust 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

PACK ConstantsUse 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

The Decimal InstructionsOrder of operationWhen you evaluate a numeric expression the order of operations govern the outcome.PEMDAS mnemonic for the priority.Parenthesis from innermost to outermostExponentiation Multiplication Division Addition Subtraction John Urrutia 2012, All Rights Reserved.145/27/2012Processedleft to rightThe Decimal InstructionsAP, SP All are storage-to-storage instructions just like PACKSets the condition code, branch Mask01 condition code = 3Overflow / Underflow02 - condition code = 2Plus / Positive04 - condition code = 1Minus / Negative08 - condition code = 0Zero14 not condition code = 3No Overflow / Underflow13 not condition code = 2Not Plus / Positive11 not condition code = 1Not Minus / Negative07 not condition code = 0Not Zero

John Urrutia 2012, All Rights Reserved.155/27/2012AP add packedSyntax of InstructionOp Code AP Operand 1 first addend (before execution) andsum (after execution)Operand 2 second addendBoth Operands must be in packed-decimal formatOperand 1 should be one byte larger than the size of Operand 2 to prevent overflow conditions.All packed-decimal instructions set the sign to XC Positive or XD Negative John Urrutia 2012, All Rights Reserved.165/27/2012AP add packedExample: CC=2 Plus value John Urrutia 2012, All Rights Reserved.175/27/2012

AP add packedExample: Data Exception Occurs (S0C7) John Urrutia 2012, All Rights Reserved.185/27/2012

AP add packed to multiplyExample: CC=2 Plus value John Urrutia 2012, All Rights Reserved.195/27/2012

Convert from PD to ZD formatUNPK (unpack) instruction converts Packed-Decimal numbers to Zone-Decimal numbers, and is storage to storage.Syntax of InstructionOp Code UNPKOperand 1 label or addressOperand 2 label or addressInstruction processes right to left John Urrutia 2012, All Rights Reserved.205/27/2012Convert from PD to ZD formatOperand 1 determines how many bytes to processThe Maximum number of bytes to unpack is 16 and so is the length of Operand 2The length needed for Operand 1 is determined by this formula but can never be longer 16 bytes:(length of operand 2)*2 1The 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/2012Convert from PD to ZD formatMixed length Operands cause the following:If Operand 1 can contain more digits than Operand 2,Operand 1 is padded to the left with zeros XF0 If Operand 1 cant contain all the digits in Operand 2,Operand 1 High-order values are truncated.

John Urrutia 2012, All Rights Reserved.225/27/2012UNPK examples John Urrutia 2012, All Rights Reserved.235/27/2012Normal 3 bytes unpack exactly into 5 bytes

UNPK examplesOperand 1 larger 2 bytes unpack into 5 bytes High-order bytes padded with zeros

John Urrutia 2012, All Rights Reserved.245/27/2012

UNPK examplesOperand 2 larger 3 bytes unpack into 3 bytes High-order byte values (1 & 2) truncated

John Urrutia 2012, All Rights Reserved.255/27/2012

UNPK examplesMaximum Operand 1 16 Bytes John Urrutia 2012, All Rights Reserved.265/27/2012

Securing the Sign The last digit of an unpacked number contain the sign of the number in the Zone portionAny 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/2012HexCharacterXC0{XC1AXC2BXC3CXC4DXC5EXC6FXC7GXC8HXC9IHexCharacterXD0}XD1JXD2KXD3LXD4MXD5NXD6OXD7PXD8QXD9RSecuring the Sign We must set the zone to FOI to the rescueOr 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/2012OI or immediateSyntax of InstructionOp Code OI Operand 1 Label or address of byte to changeOperand 2 Mask of bits to turn-onThe 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/2012OI or immediateThe Mask valueB00000000 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/2012Byte OperandXC7B11000111Mask OperandXF0B11110000Result Byte OperandXF7B11110111Each bit is turned on left to rightOI exampleCorrect character for output John Urrutia 2012, All Rights Reserved.315/27/2012

Countdown to the endWe 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.orIdentify the smallest 16 digit Fibonacci number John Urrutia 2012, All Rights Reserved.325/27/2012Factorial and FibonacciFactorialIn 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 to120 = 5 * 4 * 3 * 2 * 1ea. 6 factorial is equal to 720 = 6 * 5 * 4 * 3 * 2 * 1

John Urrutia 2012, All Rights Reserved.335/27/2012Factorial and FibonacciFibonacciIn mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relationFn = Fn-1 + Fn-2with seed values ofF0 = 0 and F1 = 1ea.F2 = 1, F3 = 2, F4 = 3, F5 = 5, F6 = 8, F7 = 13The smallest 2 digit Fibonacci number is 13. John Urrutia 2012, All Rights Reserved.345/27/2012ASM Code Sheet LABEL1OPERATION10 OPERANDS COMMENTS1672

Name ___________________________________________________CIS 020 Assembly Programming

Sheet2LABELOPERATIONOPERANDS11116APFLD1,FLD2

FLD1DCPL2'+10'FLD2DCPL1'+9'

BEFOREAFTERFLD1X'010C'FLD1X'019C'FLD2X'9C'FLD2X'9C'

Name ___________________________________________________CIS 020 Assembly Programming

ASM Code Sheet LABEL1OPERATION10 OPERANDS COMMENTS1672

Name ___________________________________________________CIS 020 Assembly Programming

Sheet2LABELOPERATIONOPERANDS11116APFLD1,FLD2

FLD1DCPL2'999'FLD2DCPL1'+1'

BEFOREAFTERFLD1X'999C'FLD1X'000C'FLD2X'1C'FLD2X'1C'

Name ___________________________________________________CIS 020 Assembly Programming

ASM Code Sheet LABEL1OPERATION10 OPERANDS COMMENTS1672

Name ___________________________________________________CIS 020 Assembly Programming

Sheet2LABELOPERATIONOPERANDS11116AP1APFLD1,FLD1AP2APFLD1,FLD1AP3APFLD1,FLD1

FLD1DCPL3'+10'BEFOREAFTERAP1FLD1X'00010C'AP1FLD1X'00020C'AP2FLD1X'00020C'AP2FLD2X'00040C'AP3FLD1X'00040C'AP3FLD3X'00080C'

Name ___________________________________________________CIS 020 Assembly Programming

ASM Code Sheet LABEL1OPERATION10 OPERANDS COMMENTS1672

Name ___________________________________________________CIS 020 Assembly Programming

Sheet2LABELOPERATIONOPERANDS11116UNPKFLD1,FLD2

FLD1DSCL5'00000'FLD2DCPL3'+12345'

BEFOREAFTERFLD1X'?????'FLD1X'F1F2F3F4C5'FLD2X'12345C'FLD2X'12345C'

Name ___________________________________________________CIS 020 Assembly Programming

ASM Code Sheet LABEL1OPERATION10 OPERANDS COMMENTS1672

Name ___________________________________________________CIS 020 Assembly Programming

Sheet2LABELOPERATIONOPERANDS11116UNPKFLD1,FLD2

FLD1DSCL5'00000'FLD2DCPL2'+123'

BEFOREAFTERFLD1X'??????????'FLD1X'F0F0F1F2C3'FLD2X'123C'FLD2X'123C'

Name ___________________________________________________CIS 020 Assembly Programming

ASM Code Sheet LABEL1OPERATION10 OPERANDS COMMENTS1672

Name ___________________________________________________CIS 020 Assembly Programming

Sheet2LABELOPERATIONOPERANDS11116UNPKFLD1,FLD2

FLD1DSCL3'000'FLD2DCPL3'+12345'

BEFOREAFTERFLD1X'??????'FLD1X'F3F4C5'FLD2X'12345C'FLD2X'12345C'

Name ___________________________________________________CIS 020 Assembly Programming

ASM Code Sheet LABEL1OPERATION10 OPERANDS COMMENTS1672

Name ___________________________________________________CIS 020 Assembly Programming

Sheet2LABELOPERATIONOPERANDS11116UNPKFLD1,FLD2

FLD1DSCL16FLD2DCPL8'123456789012345'

BEFOREAFTERFLD1X'????????????????????????????????'FLD1X'F1F2F3F4F5F6F7F8F9F0F1F2F3F4C5'FLD2X'123456789012345C'FLD2X'123456789012345C'

Name ___________________________________________________CIS 020 Assembly Programming

ASM Code Sheet LABEL1OPERATION10 OPERANDS COMMENTS1672

Name ___________________________________________________CIS 020 Assembly Programming

Sheet2LABELOPERATIONOPERANDS11116UP1UNPKFLD1,FLD2OI1OIFLD1+L'FLD1-1,X'F0'

FLD1DSCL3FLD2DCPL2'123'

BEFOREAFTERUP1FLD1X'??????'FLD1X'F1F2C3'Print Output=12CFLD2X'123C'FLD2X'123C'

OI1FLD1X'F1F2C3'FLD1X'F1F2F3'Print Output=123

Name ___________________________________________________CIS 020 Assembly Programming