Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
The Second Version (Using Stack) Push and Pop • HC11 (and all other microprocessors) has a special purpose pointer register called Stack Pointer (SP). • Stack, which is pointed by SP, is special memory place that is used for storing ands retrieving temporary data. • There are two stack operations PUSH and POP (or PULL) that we can use in our example. Week 3: Stack and Arithmetic operations • SP always points to an empty memory location to be filled next. • Push fills the empty location by a value. • In HC11, we push Accumulators A and B, IX, IY to the stack. • Each PUSH must be accompanied with a PULL in our program in order to avoid stack overflow. 1 Week 3: Stack and Arithmetic operations Stack Operation Push and Pop PUSH 1. Write value to the memory location that SP points to 2. Decrement SP $23 $01 $12 $74 2 pop push $ED (returns $ED) push $55 Empty stack POP 1. Increment SP 2. Read memory location pointed to by SP Week 3: Stack and Arithmetic operations 3 SP $55 $01 $12 $74 SP $23 $23 $ED $12 $74 $ED $12 $74 Week 3: Stack and Arithmetic operations SP SP 4 Setting up the Stack Push and Pull Instructions • The CPU assumes that the SP is pointing to an area of memory that has been reserved for stack use • It is therefore necessary for the programmer to choose where to put the stack, and to initialise the SP to the appropriate address before using it! • For a decrement-before-write CPU, the appropriate address is one higher than the highest address in the reserved area Week 3: Stack and Arithmetic operations PSHA: PSHB: PSHX: PSHY: PULA: PULB: PULX: PULY: 5 Setting up the Stack $0000 LDS #$DFFF $00FF $C000 $DFFF $E000 $FFFF Week 3: Stack and Arithmetic operations Week 3: Stack and Arithmetic operations 7 Instructions Related to the Stack Pointer (SP) • Stack grows downward, therefore, we need to set up the SP to point to the highest available RAM location. • In our lab boards: We need to equate SP to the highest RAM address SPß$DFFF Push A onto the stack Push B onto the stack Push X onto the stack (low order byte is pushed first) Push Y onto the stack (low order byte is pushed first) Pull A from the stack Pull B from the stack Pull X from the stack (high order byte is pulled first) Pull Y from the stack (high order byte is pulled first) DES: decrements the contents of the stack pointer by 1 INS: increments the contents of the stack pointer by 1 LDS: loads the contents of a memory location or an immediate value into SP STS: stores the contents of the stack pointer in a memory location TSX: places the contents of SP plus one into X TSY: places the contents of SP plus one into Y TXS: places the contents of X minus one into SP TYS: places the contents of Y minus one into SP on-chip RAM external RAM external ROM 6 Week 3: Stack and Arithmetic operations 8 Back to Our Example Second Version Stack Overflow We use SP and Push and Pop operations. This time, we are copying the bytes starting from the end of the block. • If you push more data onto the stack than will fit into the area you’ve allocated to it, then you have created a stack overflow LOOP • This is potentially disasterous, since you may overwrite (and thus destroy) important data, or instructions, outside the stack area Week 3: Stack and Arithmetic operations 9 LDS LDX LDAA PSHA DEX CPX BNE .... #$D2FF #$D0FF $0,X SP ß destination address X ß source address A ß [X + 0] [SP] ß A, SP <- SP - 1 XßX-1 #$CFFF LOOP X ?= $CFFF, changes Z flag if Z bit == 0, go to the address labeled as LOOP Week 3: Stack and Arithmetic operations 11 Stack Example Stack Overflow • Swap the contents of registers A and B • Advanced computers have special software to detect stack overflow, but simple ones do not • On a simple computer, you have to design the program to avoid it • Now you know what the ‘stack overflow’ error message you may have seen means • What might be a common cause? Week 3: Stack and Arithmetic operations LDS #$DFFF LDAA #$24 LDAB #$55 PSHA PSHB PULA PULB 10 ; ; ; ; ; ; ; ; SP ß $DFFF A ß $24 B ß $55 push $24 to stack push $55 to stack A ß $55 (top of stack) B ß $24 (top of stack) A and B are swapped Week 3: Stack and Arithmetic operations 12 Stack Example Stack Example • Stack can be used to store temporary data. For example, if you want to use accumulator A in an operation but don't want to loose its current value, you store into stack temporarily. LDS #$DFFF .... PSHA LDAA #$43 STAA $C200 PULA .... ; SP ß $DFFF ; ; ; ; TSX save A into stack temporarily use A to do something continue doing something restore A Week 3: Stack and Arithmetic operations LDAA LDAB PSHA PSHB PSHX high memory X ß SP + 1 ...... $23 $ED $12 $74 Do something PULX PULB PULA X (after TSX) SP low memory Week 3: Stack and Arithmetic operations Week 3: Stack and Arithmetic operations 15 Stack Operations Must be Symmetrical • Write down an instruction sequence to load the top element of the stack into A and the 9th element from the top of the stack into B. We need to transfer SP to one of Index registers. We can use TSX ; points the index register X ; to the top element of the stack 0,X ; places the top element of the stack in A 9,X ; places the 9th element from the top of ; the stack in B 13 Stack Example SP can't be directly used, therefore, we need to to transfer its content to IX by TSX 14 Every Push must have an accompanying Pull, the order of execution must be symmetrical. Week 3: Stack and Arithmetic operations 16 Usages of Stack • Temporary storage • Loop control Assembly Language – Can have loop variable reside on stack – Nice generic way to handle stack – Do not have to explicitly manage memory for loop variable – Will be slower compared with explicit loop variable Enabling us to understand Machine Code • Let’s have a look at array copy example Week 3: Stack and Arithmetic operations 17 Loop Control Using Stack LOOP LDAA #$06 PSHA ; Do Stuff PULA DECA BNE LOOP LOOP ; initial value ; push loop counter • Computers only understand Machine Code • We often want to be able to understand what the computer’s doing • High level languages are too far from machine code to be useful • Assembly language maps closely on to machine code, using mnemonics we can understand for the op-codes ; pull loop counter ; decrement loop counter ; initial value ; push loop counter ; pull loop counter ; decrement loop counter ; push loop counter Week 3: Stack and Arithmetic operations 19 What is Assembly Language? OR LDAA #$06 PSHA ; Do Stuff PULA DECA PSHA BNE LOOP Week 3: Stack and Arithmetic operations 18 Week 3: Stack and Arithmetic operations 20 Code layout Processor Specific • Most assemblers restrict you to one op-code per line • Blank lines are allowed (use them for clarity) • Comments are allowed (and should be used!) • Most programmers choose to lay out their assembly language programs in a specific way • The codes used are specific to a particular processor – Learning one set, however, will make it much easier to learn another set • There are assembly language mnemonics for every machine code instruction (though note addressing modes) Week 3: Stack and Arithmetic operations 21 Week 3: Stack and Arithmetic operations Typical Layout Making Machine Code Understandable • There are four areas in an assembly line • Logical syntax • Other things to make it easier to read, e.g. labels (markers in the code) • Assembly language gets converted to machine code by an assembler mylabel labels LDAA $D000 BRA mylabel op-code arguments mnemonics Labels are actually addresses Week 3: Stack and Arithmetic operations 23 22 ; load Accu A ; a loop comments semicolumn ( ; ) means beginning of a comment Week 3: Stack and Arithmetic operations 24 ORG Directive Labels • A label is simply a named location (address) • The assembler will use the location marked to insert the appropriate value into the machine code it produces • They are useful for: VAR1 NUM – branch instructions – addresses of static and global variables – the start address of functions ORG FCB FCB ORG START LDS LDAA ADDA QUIT STAA • A label refers to the address of the op-code, or data, which follows it Week 3: Stack and Arithmetic operations $D000 $32, $30 $55 VAR1 = $D000 NUM = $D002 $C000 #$DFFF VAR NUM NUM 25 START = $C000 QUIT is the address of STAA statement Week 3: Stack and Arithmetic operations 27 RMB Directive Assembler directives • RMB : reserves memory bytes • The assembler can do more than just produce the machine code • You can use it to reserve memory locations for data storage, and supply initial values for that data • The commands you use to do this are called assembler directives. These are not mnemonics Week 3: Stack and Arithmetic operations Do NOT forget: Labels are addresses ORG directive determines at which memory location to start putting machine code. – reserve memory bytes without initialization – syntax is [<label>] RMB <expression> [<comment>] • The statement BUFFER RMB 100 allocates 100 bytes for data and can be referred to by the label “BUFFER”. in C Language int buffer[100]; 26 Week 3: Stack and Arithmetic operations 28 FCB Directive FCC Directive • FCB : Form Constant Byte • FCC : Form Constant Character – reserves as many bytes as the number of arguments in the directive – each argument specifies the initial value of the corresponding byte – generates ASCII code bytes for the letters in the arguments • syntax is [label] • syntax is • The directive [<label>] FCB <exprs>][,<exprs>,...,<exprs>][<comment>] FCB [<comment>] char ALPHA[]="ABC"; ALPHA FCC "DEF" • The statement ABC FCC "string" will generate the following values in memory $44 $45 $46 (ASCII codes of D, E, F) $11,$22,$33 reserves three consecutive memory bytes and initializes their values to $11, $22, and $33. in C Language: char ABC[3]={0x11,0x22,0x33}; Week 3: Stack and Arithmetic operations 29 Week 3: Stack and Arithmetic operations FDB Directive BSZ Directive • FDB : Form Double Byte – reserves 2 bytes for each argument of the directive – each argument specifies the initial value of the corresponding double bytes • BSZ : Block Storage of Zeros – causes the assembler to allocate a block of bytes that are initialized to zeros • syntax is • syntax is [<label>] FDB [<label>] BSZ <expression> [<exprs>][,<exprs>,...,<exprs>] [comment>] FDB [<comment>] • The statement • The directive ABC 31 buffer BSZ $11,$22,$33 80 • reserves a block of 80 bytes and their values are initialized to 0. will initialize 6 consecutive bytes in memory to $00 $11 $00 $22 $00 $33 in C Language: int ABC[3]={0x11,0x22,0x33}; Week 3: Stack and Arithmetic operations 30 Week 3: Stack and Arithmetic operations 32 DCB Directive EQU Directive • DCB : Define Constant Block • EQU : Equate – reserve an area of memory and initialize each byte to the same constant value – not supported by the Motorola freeware as11.exe – allows the user to use a symbolic name in place of a number • syntax is • syntax is <label> [label] DCB <length>,<value> DCB ROM 80, $20 EQU $E000 #define ROM 0xE000 tells the assembler that wherever ROM appears in the program, the value $E000 is to be substituted. will generate a line of 80 space characters. Week 3: Stack and Arithmetic operations <expression> [<comment>] • The directive • The directive space EQU 33 Week 3: Stack and Arithmetic operations 35 FILL Directive • FILL -- fill a block of constant values – serve as the same purpose as does the DCB directive • syntax [<label>] FILL Examples <value>,<length> • The directive ONES FILL 1, 40 Various Examples • will force the freeware assembler to fill each of the 40 memory locations with a 1. Week 3: Stack and Arithmetic operations 34 Week 3: Stack and Arithmetic operations 36 Example 1: Discussion of the Solution Example – 1 • Write a program to append a string (STRING1) to the end of another string (STRING2). Use C Language string convention, i.e. character 0 at the end. • Solution: There are two steps in this program, • LDAA modifies the flags according to the value loaded into ACCA. Therefore, no need to perform a comparison COPY – Find the end of the first string – Copy string 1 beginning from the end of string 2 Week 3: Stack and Arithmetic operations NEXT COPY DONE $C000 LDX LDAA BEQ INX BRA LDY LDAA STAA BEQ INX INY BRA SWI #STR2 0,X NEXT LOOP #STR1 0,Y 0,X DONE COPY ; ; ; ; ; ; ; program memory X ß string 2 loop until char = 0 ??? next char back to loop Y ß string 1 ; ; ; ; ; ; ; A ß [string1] string2 ß string1 check for the end next char in string2 next char in string1 loop return to monitor Week 3: Stack and Arithmetic operations 0,Y 0,X #$0 DONE COPY LDAA STAA BEQ 0,Y 0,X DONE LDAA is modifying Z flag, STAA does not destroy it. 37 Example 1: Solution ORG FIND LOOP LDAA STAA CMPA BEQ Week 3: Stack and Arithmetic operations 39 Example 2 : List Exchange • Write a program segment to exchange two arrays A1 and A2 of length 10. • Declare data segment and program segment using EQU directives. • Declare A1 and A2 in data segment with FCB directives. 38 Week 3: Stack and Arithmetic operations 40 DATA MAIN EQU EQU A1 A2 ORG FCB FCB INIT AGAIN EXIT ORG LDAA LDX LDY PSHA LDAA LDAB STAA STAB INX INY PULA DECA BNE SWI $D000 $C000 ;Data segment begins at $D000 ;Executable program begins at $C000 Solution DATA $41,$74,$20,$41,$31,1,2,3,4,5 ;Initial A1 $41,$74,$20,$41,$32,6,7,8,9,10 ;Initial A2 MAIN #$0A #A1 #A2 0,X 0,Y 0,Y 0,X AGAIN ;Initialize for 10 iterations ;Set up index address pointer to A1 ;Set up index address pointer to A2 ;Store loop counter on stack ;First get byte from A1 ;Next get byte from A2 ;Store byte from A1 in A2 ;Store byte from A2 in A1 ;increment A1 pointer ;increment B2 pointer ;retrieve loop counter from stack ;decrement loop counter ;branch back if counter not zero ;else exit Week 3: Stack and Arithmetic operations 41 ORG SQTAB FCB $D000 0, 1, 4, 9, 16, 25, 36, 49 LDX LDAA BEQ CONT INX DECA BNE FOUND LDAA STAA SWI #SQTAB $D040 FOUND CONT $00,X $D041 load the base of the table get the input data if it is zero, stop point to the next entry decrement A and repeat for ($D040) times get the corresponding entry store the result Week 3: Stack and Arithmetic operations 43 Example 3 • Square from a lookup table: Write a program to find the square of a 3-bit binary number from a look up table. • The table starts at SQTAB • The location $D040 contains the number whose square is required. • The result will be saved in location $D041. Week 3: Stack and Arithmetic operations Cross Assembling Using programs to assemble 42 Week 3: Stack and Arithmetic operations 44 S19 File Cross Assembly S104103500B6 S105FFD6D0460F S105FFD8D12C26 S105FFDAD1311F S105FFDCD13618 S105FFDED13B11 S105FFE0D1400A S105FFE2D14503 S105FFE4D14AFC S105FFE6D12024 S105FFE8D14FF3 S105FFEAD154EC S105FFECD159E5 S105FFEED15EDE S105FFF0D0EA51 S105FFF2D163D5 S105FFF4D168CE S105FFF6D16DC7 S105FFF8D172C0 S105FFFAD177B9 S105FFFCD17CB2 S105FFFED0002D S123D0008603B710248E003F7F102CCC302CB7102BF7102D8603B710268640B71024860213 S123D020B71039860BB7103F0EBDD0918600C600DD419740BDD0B6BDD0CC8655B7103A86EA S123D040AAB7103A20F43BF6102E2AFBB7102F39FC100EC30000FD10188640B71023B710D1 S123D0602239CE0000FC100EC30000FD10168680B71023B61023848027F939CE0000FC1073 S123D0800EC3000020E5CE0000FC100EC3000020DA36868BB710268600B7100CB710008637 S123D0A000B710048600B71007323936B610048A20B71004323936B6100484DFB7100432A2 S123D0C03936B610048A40B71004323936B6100484BFB710043239B6100084EFB710008A10 S123D0E010B7100084EFB7100039B610258A40B71025DC41C30001DD41C1202D224F9741EB S123D10097429640270DBDD0B64F97408646BDD047200CBDD0AB86FF9740864EBDD0473B7C S123D1207F10228640B710238D013B3986017ED18186027ED18186037ED18186047ED18126 S123D14086057ED18186067ED18186077ED18186087ED18186097ED181860A7ED181860B09 S123D1607ED181860C7ED181860D7ED181860E7ED181860F7ED18186107ED18186117ED101 S105D180813BED S9030000FC • Cross assembly means, using an assembler on a system to produce machine code for another system. • You are provided AS11.EXE to assemble for HC11 system. • You type a program on a text editor and save with extension ".asm". as11 prog.asm –l > prog.lst Week 3: Stack and Arithmetic operations 45 47 S19 Record Type Cross Assembly Type Count • as11.exe produces two files, namely, "prog.s19" and "prog.lst" • s19 file contains the machine code instructions to be loaded into a HC11 system. It is a kind of code locating scheme. • lst file, contains what you type in the editor and corresponding machine code for each code line. Week 3: Stack and Arithmetic operations Week 3: Stack and Arithmetic operations Address Data Checksum • Type: A char[2] field. These characters describe the type of record (S0, S1, S2, S3, S5, S7, S8, or S9). • Count: A char[2] field. These characters when paired and interpreted as a hexadecimal value, display the count of remaining character pairs in the record. • Address: A char [4,6, or 8] field. These characters grouped and interpreted as a hexadecimal value, display the address at which the data field is to be loaded into memory. The length of the field depends on the number of bytes necessary to hold the address. 46 Week 3: Stack and Arithmetic operations 48 S-Fields S19 Record Type Type Count Address Data • S1 Record : The type of record field is 'S1' (0x5331). The address field is interpreted as a 2-byte address. The data field is composed of memory loadable data. • S2 Record: The type of record field is 'S2' (0x5332). The address field is interpreted as a 3-byte address. The data field is composed of memory loadable data. • S3 Record : The type of record field is 'S3' (0x5333). The address field is interpreted as a 4-byte address. The data field is composed of memory loadable data. • S5 Record : The type of record field is 'S5' (0x5335). The address field is interpreted as a 2-byte value and contains the count of S1, S2, and S3 records previously transmitted. There is no data field. Checksum • Address (cont): A 2-byte address uses 4 characters, a 3-byte address uses 6 characters, and a 4-byte address uses 8 characters. • Data: A char field [size of 0-64 bytes]. These characters when paired and interpreted as hexadecimal values represent the memory loadable data or descriptive information. • Checksum: A 2 char field. These characters when paired and interpreted as a hexadecimal value display the least significant byte of the ones complement of the sum of the byte values represented by the pairs of characters making up the count, the address, and the data fields. • Note: Each record is terminated with a carriage return/line feed sequence. Week 3: Stack and Arithmetic operations 49 • S7 Record : The type of record field is 'S7' (0x5337). The address field contains the starting execution address and is interpreted as 4-byte address. There is no data field. • S8 Record : The type of record field is 'S8' (0x5338). The address field contains the starting execution address and is interpreted as 3-byte address. There is no data field. • S9 Record : The type of record field is 'S9' (0x5339). The address field contains the starting execution address and is interpreted as 2-byte address. There is no data field. • The type of record is 'S0' (0x5330). The address field is unused and will be filled with zeros (0x0000). The header information within the data field is divided into the following subfields. Mname is char[20] and is the module name. Ver is char[2] and is the version number. Rev is char[2] and is the revision number. Description is char[0-36] and is a text comment. • Each of the subfields is composed of ASCII bytes whose associated characters, when paired, represent one byte hexadecimal values in the case of the version and revision numbers, or represent the hexadecimal values of the ASCII characters comprising the module name and description. Week 3: Stack and Arithmetic operations 51 S-Fields S0 Record – – – – Week 3: Stack and Arithmetic operations 50 Week 3: Stack and Arithmetic operations 52 Intel Hex Format S19 File Example • Similar machine code format is the Intel Hex Format. S00600004844521B S1130000285F245F2212226A000424290008237C2A S11300100002000800082629001853812341001813 S113002041E900084E42234300182342000824A952 S107003000144ED492 S5030004F8 S9030000FC :10000000DB00E60F5F1600211100197ED300C3004C :1000100000000101030307070F0F1F1F3F3F7F7FF2 :01002000FFE0 :00000001FF • Format: – – – – – – S00600004844521B S1130000285F245F2212226A000424290008237C2A Type= S1 Type = S0 Length = 13hex bytes (19 decimal bytes) Length= 06 byes (hex) Address = 0000 (irrelevant in S0) Address = 0000 (absolute) Data = 28 5F 24 5F ..... 23 7C Name= “HDR” Checksum= 2A Checksum= 0x1B Week 3: Stack and Arithmetic operations 53 Start Character Byte Count (# of data bytes) Address of first data Record Type (00 data, 01 end of record) Data Bytes Checksum Week 3: Stack and Arithmetic operations 55 S19 File Example S5030004F8 Type = S5 Length = 03hex bytes Address = 0004 (absolute) meaning 4 S1 records have been sent prior to this S5 record Checksum = F8 Arithmetic Operations S9030000FC Type= S9 Length=03hex bytes Address = 0000 (absolute- hexidecimal) indicating the starting execution address Checksum= FC Week 3: Stack and Arithmetic operations ADDition, SUBtraction, MULtiplication, DIVision 54 Week 3: Stack and Arithmetic operations 56 Arithmetic Instructions Example • All µPs include a number of basic arithmetic operations • Many 8-bit µPs actually include only addition and subtraction instructions (all other more complex arithmetic operations must then be carried out by performing appropriate sequences of these instructions) • The 68HC11 also include multiplication and division instructions for improved efficiency • Square roots, trigonometric functions and other complex operations must still be carried out by writing appropriate subroutine functions • Assume that both accumulators are holding unsigned data. Write an instruction sequence that starts at address C000 and branches to C06F if [ACCA] is larger than [ACCB]; otherwise, the program halts. Week 3: Stack and Arithmetic operations SBA ; Subtract accumulators BHI $C06F ; Branch to C06F if ; [ACCA] > [ACCB] SWI ; Halt 57 Week 3: Stack and Arithmetic operations 59 68HC11 Arithmetic Operations 68HC11 Arithmetic Instructions • ABA • ADDA / ADDB / ADDD Mem / Imm – Adds [ACCA] to [ACCB] and places the result in [ACCA] – Adds the specified operand to the contents of the specified accumulator and places the result into the same accumulator • ADCA / ADCB Mem / Imm • ABX / ABY – Add with carry, same as ADDA and ADDB except that the carry flag is included in the addition (used for multi-byte additions where the data are larger than a single byte) – Adds [ACCB] to the contents of index register X or Y and places the result in the same index register • SUBA / SUBB / SUBD / SBCA / SBCB • SBA – These are the same as the equivalent addition instructions except that subtraction operations are performed instead. Note: After a subtract operation is performed, the C flag is complemented so that it indicates the occurrence of a borrow – Subtracts ACCB from ACCB and puts the result in ACCA Week 3: Stack and Arithmetic operations 58 Week 3: Stack and Arithmetic operations 60 Multiple Precision Addition Binary Multiplication • How can we add or subtract numbers that are more than 2 bytes? • This is accomplished using 8 bit addition and subtraction operations using carry or borrow. HIGH1 LOW1 HIGH2 LOW2 RMB RMB RMB RMB 1 1 1 1 LDAA ADDA STAA LDAA ADCA STAA LOW1 LOW2 LOW2 HIGH1 HIGH2 HIGH2 11000 multiplicand 10001 multiplier 11000 If multiplier bit is 1, 00000 copy the 00000 multiplicand to the 00000 below. i.e. Add and Shift + 11000 110011000 = 408 high byte of number 1 low byte of number 1 high byte of number 2 low byte of number 2 add low bytes carry may be produced STAA does not destroy C LDAA does not destroy C add with carry store result Week 3: Stack and Arithmetic operations 61 8-bit Multiplication 24 17 + + Clear ANS ANS: running sum Shift Right Multiplier Double Precision Shift Multiplicand 168 24 408 Week 3: Stack and Arithmetic operations 63 8-bit Binary Multiplication • We will perform 16-bit x 16-bit multiplication using MUL instruction. • 68HC11 does have the capability to multiply two 8bit numbers. For understanding what happens, we will multiply 2 8-bit numbers. Decimal Multiplication Week 3: Stack and Arithmetic operations Carry ? Yes Double Precision Add Multiplicand to ANS No No MPR=0? Yes DONE 62 Week 3: Stack and Arithmetic operations 64 8-bit Multiplication ANSRHI ANSRLO DPACHI DPACLO MUL RMB RMB RMB RMB CLR CLR CLR STAA LOOP LSRB BCC PSHB LDD ADDD STD PULB DTEST TSTB BEQ ASL ROL BRA 1 1 1 1 ANSRHI ANSRLO DPACHI DPACLO DTEST ANSRHI DPACHI ANSRHI DONE DPACLO DPACHI LOOP Alternate 8-bit Multiplication ; ANSwer MCAND RMB 1 MPR RMB 1 ANSRHI FCB 0 ANSRLO FCB 0 ; assumes A is the Multiplier, B is the multiplicand MUL STAA MPR STAB MCAND CLRA LDAB #$08 loop count LOOP ROR MPR bit set? BCC SKIP if not, do not add. ADDD MCAND SKIP RORA double precision shift right ROR ANSRLO DECB decrement loop count BNE LOOP STAA ANSRHI ANSRLO has been filled by rotate ; accumulator ; clear result ; clear double precision accu. ; ; ; ; ; ; ; ; ; ; ; shift B by one bit if C==0, goto DTEST save B add DPAC to ANS " " " " " " restore B check status of B if multiplier is 0 then done shift left multiplicand do it for 16bits Week 3: Stack and Arithmetic operations 65 Week 3: Stack and Arithmetic operations 67 68HC11 Arithmetic Instructions Alternate 8-bit Multiplication • MUL • Instead of shifting left the answer in each iteration, put the answer in high byte and rotate right 8 times. – Multiplies [ACCA] by [ACCB] and places the result in ACCD by hardware • IDIV – The IDIV instruction divides [ACCD] by [X], the quotient is placed in X and the remainder in ACCD. A 0 0 A • FDIV (Fractional Division) SHIFT RIGHT 8 TIMES Week 3: Stack and Arithmetic operations – The FDIV instruction is used for cases when the numerator is less than the denominator, resulting in a fractional quotient. 66 Week 3: Stack and Arithmetic operations 68 16bit Multiplication 16bit Multiplication – Step 1 • We can use MUL instruction to perform the multiplication of two 16-bit numbers. X X 89AB CDEF EFxAB EFx89 CDxAB CDx89 ????????????? 16-BIT MULTIPLICAND MChi MClo MPhi MPlo MClo x MPlo 16-BIT MULTIPLIER 8-BITS SHIFT 8-BITS SHIFT The result is 16bits, it goes to ANS1 and ANS0 directly 16-BITS SHIFT 32-BIT RESULT Week 3: Stack and Arithmetic operations 69 16bit Multiplication Week 3: Stack and Arithmetic operations 71 16bit Multiplication – Step 1 • Memory usage MPHI MPLO MCHI MCLO ANS3 ANS2 ANS1 ANS0 RMB RMB RMB RMB RMB RMB RMB RMB 1 1 1 1 1 1 1 1 Assuming, these values are already loaded into memory Assuming, the result, which is 32 bits will be stored here Week 3: Stack and Arithmetic operations 70 MPY16 LDD #0 STD ANS3 ANS3:ANS2 ß 0 STEP1 LDAA LDAB MUL STD A ß Multiplier low B ß Multiplicand low multiply ACCA and ACCB result à ANS1:ANS0 MPLO MCLO ANS1 Week 3: Stack and Arithmetic operations 72 16bit Multiplication 16bit Multiplication – Step 2 MChi MClo X MPhi MPlo MClo x MPlo MChi x MPlo Do we obtain carry after addition? NO !!!! Why not? • Let us assume that multiplier and multiplicand are the largest available 16 bit numbers, i.e. $FFFF. • Let us carry on the operation and see if carry is obtained. • The result of $FF x $FF is $FE01 ANS2 The result is 16bits, it goes to ANS2 and ANS1, but we need to add to the previous result of MClo x MPlo + NO CARRY 73 16bit Multiplication : Step 2 MPLO MCHI ANS2 ANS2 00 FE FE 01 FE FF ANS2 Week 3: Stack and Arithmetic operations STEP2 LDAA LDAB MUL ADDD STD ANS1 ANS0 01 ANS1 ANS0 Week 3: Stack and Arithmetic operations 75 16bit Multiplication – Step 3 MChi MClo X MPhi MPlo MClo x MPlo MChi x MPlo MClo x MPhi A ß Multiplier low B ß Multiplicand high multiply ACCA and ACCB result += $00:ANS1 store the summation We can't have a carry. Week 3: Stack and Arithmetic operations 01 Do we have carry? YES!!!! 74 The result is 16bits, it goes to ANS2 and ANS1, we add to the previous result. Week 3: Stack and Arithmetic operations 76 16bit Multiplication : Step 4 16bit Multiplication – Step 3 Assuming maximum numbers ANS2 + NO CARRY + CARRY 00 FE FE 01 FE FF FE 01 FD ANS3 ANS2 MChi MClo X MPhi MPlo MClo x MPlo MChi x MPlo MClo x MPhi MChi x MPhi ANS1 ANS0 00 01 RESULT OF 2nd STEP ADDITION 01 RESULT OF 3rd STEP ADDITION 01 Do we have carry? NO!!!! ANS1 ANS0 Week 3: Stack and Arithmetic operations 77 Week 3: Stack and Arithmetic operations 16bit Multiplication : Step 3 x A ß Multiplier low B ß Multiplicand high multiply ACCA and ACCB result += ANS2:ANS1 store the summation store carry in ANS3 MPLO MCHI ANS2 ANS2 ANS3 STD ANS3 does not destory CARRY bit. + FF FF FF FF FE 01 FE 01 FE 01 FE 01 FF FE 00 01 This is the maximum number that can be obtained as a result of multiplication of $FFFF and $FFFF The maximum number does not exceed 32 bit number boundary. Therefore no carry happens. We DO have a carry. ROL rolls the carry into ANS3 Week 3: Stack and Arithmetic operations 79 16bit Multiplication : Step 4 ; No carry from the previous step STEP3 LDAA LDAB MUL ADDD STD ROL The result is 16bits, it goes to ANS3 and ANS2, we add to the previous result. 78 Week 3: Stack and Arithmetic operations 80 Logical Operations 16bit Multiplication : Step 4 STEP4 LDAA LDAB MUL ADDD STD MPHO MCHI ANS3 ANS3 • The 68HC11 include a number of instructions that perform bitwise logical operations • ANDx / ORAx / EORx Mem / Imm A ß Multiplier high B ß Multiplicand high multiply ACCA and ACCB result += ANS3:ANS2 store the summation Week 3: Stack and Arithmetic operations – This group of instructions takes the contents of the specified accumulator (where x is either A or B) and the specified operand and performs the specified logic operation bit-by-bit on those values, the result is stored in the same accumulator 81 Week 3: Stack and Arithmetic operations 83 Example: AND Masking • One of the most common uses for the AND instructions is to selectively clear specific bits of a data word. • This is done by ANDing the data word with another word, called a mask, which contains 0s in the bit positions to be cleared and 1s everywhere else • For example: Logical Operations AND, OR, Exclusive OR LDAA #$32 ; ANDA #$0f ; ; The lower 4 bits of ; and the rest is set Week 3: Stack and Arithmetic operations 82 A ß %00110010 A ß A & %00001111 is kept, to 0, which is $02 Week 3: Stack and Arithmetic operations 84 Example: Selective Inversion Bit Picking 1 0 1 1 0 0 1 • The EOR (Exclusive OR) instructions can be used to selectively invert specific bits of a data word, the mask should contain 1s at the positions of the bits to be inverted • This can also be used in order to completely invert a data word on a 68HC11 as shown in the example below where an input device that places inverted data on the data bus is read from: 1 * assuming ACCA is set ANDA #$02 BNE SKIP LDAA #$FF BRA SKP2 SKIP CLRA SKP2 Check whether this bit is zero. If it is zero, put $FF to ACCA, otherwise 0. Week 3: Stack and Arithmetic operations LDAA #$32 ; A ß %00110010 EORA #$FF ; A ß A EOR %11111111 ; All bits of the number $32 are inverted, ; which is $CD (COMA can also be used) 85 Example: OR Masking Week 3: Stack and Arithmetic operations 87 Bit Manipulation • Similarly, OR instructions can be used to selectively set specific bits of a data word • The OR mask should have 1s in the bit positions to be set and 0s in all other positions • For example: • BCLR: Clear Bits (M) – M = M & (mask) • BSET: Set Bits (M) –M = M | (mask) LDAA #$32 ; A ß %00110010 ORA #$0f ; A ß A OR %00001111 ; The lower 4 bits of number $32 is set ; to 1, which yields $3F Week 3: Stack and Arithmetic operations 86 Week 3: Stack and Arithmetic operations 88 Logical Shift Operations LSLA: Logical Shift Left A C Shift and Rotate Instructions 7 6 5 4 3 2 1 0 0 Example: LDAA #$3B ; A ß %00111011 LSLA ; A becomes %01110110 ; which is $76 Bitwise shifting and rotating of bytes Week 3: Stack and Arithmetic operations 89 Shift Operations Week 3: Stack and Arithmetic operations 91 Logical Shift Operations LSRB: Logical Shift Left B Arithmetic shift operations: keep the sign bit intact and performs the shift operations. ASL (Arithmetic Shift Left) ASR (Arithmetic Shift Right) 0 Rotate operations: are used in special operations. ROL: Rotate Left ROR: Rotate Right C Example: LDAB #$96 ; B ß %10010110 LSRB ; B becomes %01001011 ; which is $4B Logical shift operations: treat numbers as they are (i.e. unsigned) and performs the shifting. LSL: Logical Shift Left LSR: Logical Shift Right Week 3: Stack and Arithmetic operations 7 6 5 4 3 2 1 0 90 Week 3: Stack and Arithmetic operations 92 Rotate Operations Arithmetic Shift Operations ASLA: Arithmetic Shift Left A C ROLB: Rotate Left B 7 6 5 4 3 2 1 0 0 C Example: Example: LDAA #$3B ; A ß %00111011 ASLA ; A becomes %01110110 ; which is $76 SEC ; LDAB #$3B ; ROLB ; ; ASLA is identical to LSLA. Week 3: Stack and Arithmetic operations 93 set carry bit B ß %00111011 B becomes %01110111 which is $77 Week 3: Stack and Arithmetic operations Arithmetic Shift Operations 95 Rotate Operations RORB: Rotate Right B ASRB: Arithmetic Shift Right B 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 C C Example: 7 6 5 4 3 2 1 0 Example: LDAB #$96 ; B ß %10010110 ASRB ; B becomes %11001011 ; which is $CB Week 3: Stack and Arithmetic operations CLC ; LDAB #$BB ; RORB ; ; 94 clear carry bit B ß %10111011 B becomes %01011101 which is $5D Week 3: Stack and Arithmetic operations 96 Solution 16 bit Shift and Rotate LDAA $C101 BEQ DONE BMI DIVI LOOP1 LSL $C100 DECA BNE LOOP1 BRA DONE DIVI NEGA LOOP2 LSR $C100 DECA BNE LOOP2 DONE ...... • Shift operations can also be performed on Accumulator D (i.e. 16 bit) – LSLD, LSRD: 16 bit logical shift – ASLD, LSRD: 16 bit arithmetic shift • There is no 16 bit rotation operation (no need for that) Week 3: Stack and Arithmetic operations 97 Week 3: Stack and Arithmetic operations Example 99 Example • Write a program segment that multiplies/divides by powers of 2. • Write a program to shift the 32-bit number stored at $20-$23 to the right four places. – Operand is at address $C100 (what is manipulated) – Argument is at $C101 (by how much) • if positive: means powers of 2 multiplication • if negative: means powers of 2 division • if zero: do nothing again • If operand is 25 and argument is 2, the result is 100 (i.e. 25 x 2^2) • If operand is 25 and argument is –3, the result is 3 (i.e. 25 / 2^3) Week 3: Stack and Arithmetic operations Get the argument If zero, do nothing if minus, goto DIVI logical sift left operand decrement loop counter if end is not reached, LOOP1 if loop counter is zero quit if argument is -ve, negate logical sift right decrement loop counter if end is not reached, LOOP2 98 ldab ldx #4 #$20 lsr ror ror ror decb bne 0,X 1,X 2,X 3,X ; set up the loop count ; use X as the pointer ; to the left most byte again Week 3: Stack and Arithmetic operations 100 Comparison • CMPA / CMPB Mem / Imm – The compare instructions perform a pseudosubtraction of the operand from [ACCA] or [ACCB], the N, C, V and Z flags are set according to the result but it is not actually stored anywhere – The flags can then be checked using an appropriate branch instruction to determine the relative values of the operands (i.e. A = B, A < B or A > B) Compare and Test Operations Comparison and testing are two important operations in assembly language programming • CBA – As above but subtracts [ACCB] from [ACCA] Week 3: Stack and Arithmetic operations 101 Week 3: Stack and Arithmetic operations 103 Comparison Compare and Test • BITA / BITB Mem / Imm Many instruction sets contain two special instructions for use with conditional branches: – The bit test instructions perform a bit-by-bit pseudoAND operation between the operand and [ACCA] or [ACCB], the result is used to affect the N and Z flags but is not itself placed in any register – This means that AND masking can be used to isolate one or more bits in a register without affecting its contents • Compare: acts like a subtraction, but does not produce a result. Condition codes set accordingly • Test: like compare, but only one argument, whose value is compared with zero • TST Mem – This instruction examines the data in the specified memory location and adjusts the N and Z flags accordingly • TSTA / TSTB Mem / Imm – As above but operates on either [ACCA] or [ACCB] Week 3: Stack and Arithmetic operations 102 Week 3: Stack and Arithmetic operations 104 Example of TST • You have a list of signed numbers starting at address $D101. • Address $D100 contains the length of the list. • Write a program segment (starting at address $C000) that finds how many numbers that are not zero (0). • It returns the value in Accumulator A. Week 3: Stack and Arithmetic operations 105 Solution SUBR LOOP SKIP ORG $C000 sets starting address LDAB CLRA LDX TST BEQ INCA INX DECB BNE ... $D100 load length clear counter beginning of list test!!! (compares with zero) if not zero, skip increment counter #$D101 0,X SKIP LOOP decrement list counter if not EOL, goto beginning Accu A returns TST 0,X (fast) can be replaced by LDAA 0,X CMPA #0 Week 3: Stack and Arithmetic operations 106