1. Trang chủ
  2. » Công Nghệ Thông Tin

Hardware and Computer Organization- P9 pps

30 579 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 484,31 KB

Nội dung

Next, assemble the program and, using the list file, set a breakpoint at the instruction in the subroutine where the data value is written to memory.. Using the trace instruction write th

Trang 1

MOVEM.W (SP)+,A3/D1/D7 * Restore the registers

The data storage region contains out test patterns and the reserved memory for holding the count and the bad addresses Notice that the END directive comes at the end of all of the source code, not just the program code The value defined by end_tests is similar to the NULL character that we use to terminate a string in C Each time through the test loop we check for this character to see if the program is done

* Data storage region

tests DC.B test1,test2,test3,test4,end_tests * tests

bad_cnt DS.W 1 * counter for bad locations

bad_addr DS.W 10 * save space for 10 locations

Suggested Exercise

Carefully read the code and then build a flow chart to describe how it works Next, create a source file and run the program in the simulator In order to test the program, change the ending address for the test to something reasonably close to the beginning, perhaps 10 or 20 bytes away from the start Next, assemble the program and, using the list file, set a breakpoint at the instruction in the subroutine where the data value is written to memory Using the trace instruction write the data value to memory, but then change the value stored in memory before starting to trace the program again In other words, force the test to fail Watch the program flow and confirm that it is behaving the way you would expect it to If you are unsure about why a particular instruction or addressing

mode is used, review it in your notes or refer to your Programmer’s Reference Manual Finally,

using this program as a skeleton, see if you can improve on it using other addressing modes or instructions Please give this exercise a considerable amount of time It is very fundamental to all

of the programming concepts that we’ve covered so far

Summary of Chapter 8

Chapter 8 covered:

• How negative and real numbers are represented and manipulated within a computer

• Branches and the general process of conditional code execution based upon the state of the flags in the CCR

• The primary addressing modes of the 68K architecture

• High level language loop constructs and their analog in assembly language

• Using subroutines in assembly language programming

• A detailed walk-through of an assembly language program to test memory

Chapter 8: Endnotes

1 Alan Clements, 68000 Family Assembly Language, ISBN 0-5349-3275-4, PWS Publishing Company, Boston,

1994, p 29

Trang 2

1 Shown below on the right is a schematic diagram of a 7-segment display The table on the left represents the binary code that displays the corresponding digits on the display Thus, to illuminate the number ‘4’ on the display, you would set DB1, DB2,DB5 and DB6 to logic level 1, and all the other data bits to logic level 0.

• The display is memory-mapped to byte

address $1000

• There is a hardware timer located at

ad-dress $1002

• The timer is started by writing a 1 to

DB4 DB4 is a write-only bit and reading

from it always gives DB4=0

• When the timer is started DB0 (BUSY)

goes low and stays low for 500

millisec-onds After 500 milliseconds, the timer

times-out and DB0 goes high again

• DB0 is read-only and writing to it has no

effect on the timer All other bit positions

may be ignored The timer control register

is shown schematically, right:

Write a short 68K assembly language

sub-routine that will count down to zero from the

number passed into it in register D0.B The current state of the count down is shown on the seven-segment display The count down rate is one digit every two seconds

Notes:

• This is a subroutine There is no need to ORG your program, set-up a stack pointer or use and END pseudo-op.

• You may assume that number passed-in is in the range of 1 to 9 You do not have to do any error checking

The subroutine is exited when the counter reaches 0.

2 Assume that some external device has transmitted a sequence of byte values to your computer

Along with the sequence of bytes the external device transmits a checksum value that you

will use to determine if the byte sequence that you received is exactly the same as the byte sequence that was transmitted To do this you will calculate the same checksum for the byte stream that you received and then compare it with the checksum that was transmitted to you If they are equal, then it is extremely likely that there was no error in transmission

Exercises for Chapter 8

Bit Pattern-Binary COUNT DB7 DB0

DB4

DB3 DB6

X = Don’t Care DB7 DB6 DB5 DB4 DB3 DB2 DB1 DB0

Trang 3

Part A: Write an assembly language subroutine, not a program, which will calculate a

check-sum for a sequence of bytes located in successive memory locations The checkcheck-sum is simply

a summation of the total value of the bytes, much like summing a column of numbers The checksum value is a 16-bit value Any overflow or carry beyond 16-bits is ignored

1 The information that the subroutine needs is passed into the subroutine as follows:

a Register A0 = Pointer to the byte string in memory, represented as a long word.

b Register D0 = Checksum passed to the subroutine for comparison, represented as a

word value

c Register D1 = Length of byte sequence, represented as a word value.

2 Any overflow or carry generated by the checksum calculation past 16 bits is ignored Only

the word value obtained by the summation is relevant to the checksum

3 If the calculated checksum agrees with the transmitted value, then address register A0 returns a pointer to the start of the string

4 If the checksum comparison fails, the return value in A0 is set to 0

5 With the exception of address register A0, all registers should return from the subroutine with their original values intact

Part B: What is the probability that if there was an error in the byte sequence, it wouldn’t be detected by this method?

3 What is the value in register D0 after the highlighted instruction has completed?

00000422 60FE STOP_HERE BRA STOP_HERE

4 Write a subroutine that conforms to the following specification The subroutine takes as its

input parameter list the following variables:

• A longword memory address in register A0, where A0 points to the first element of a sequence of 32-bit integers already present in memory

• A 32-bit longword value in register D1, where the value in D1 is a search key

• A positive number between 1 and 65,535 in register D0, where the value in D0 determines how many of the integer elements in the sequence pointed to by A0 will be searched

• The subroutine returns in register D2 the value zero, if there is no match between the search key and the numbers in the sequence being searched; or the numeric value of the memory location where the first match is found

Note that once a match occurs there is no need to keep searching and you should assume that you have no knowledge of the state of the rest of the program that is calling your subroutine

Trang 4

Programming in Assembly Language

5 The memory map of a certain computer system consists of ROM at address 0000 through

0x7FFF and RAM at address 0x8000 through 0xFFFF There is a bug in the following snippet

of code What is it?

a Operand 1: High order 32-bits stored in memory location $1000

b Operand 1: Low order 32-bits stored in memory location $1004

c Operand 2: High order 32-bits stored in memory location $1008

d Operand 2: Low order 32-bits stored in memory location $100C

Store the result as follows:

a High order 32-bits in memory location $1020

b Low order 32-bits in memory location $1024

Any carry out generated by the high order addition in memory location $101F

7 The diagram shown below represents a circular array of eight lights that are connected to an 8-bit, memory mapped, I/O port of a 68K-based computer system Each light is controlled by

a corresponding bit of the I/O port Writing a 1 to a bit position will turn on the light, writing a

0 will turn it off

Write a short 68K assembly language program that will turn on each lamp in succession, keep it

on for two seconds, turn it off and then turn on the next lamp The specifications are as follows:

• The 8-bit wide parallel I/O port is mapped at

mem-ory address $4000

• There is a 16-bit wide time-delay port located

at memory address $8000 DB0 through DB11

represent a count-down timer that can generate

a time delay Writing a value to DB0-DB11 will

cause DB15 to go from low to high The timer

then counts down from the number stored in

DB0-DB11 to zero When the timer reaches zero, DB15

goes low again and the timer stops counting Each

timer tick represents 1 millisecond Thus, writing

$00A to the timer will cause the timer to count down for 10 milliseconds

• DB15 is a read-only bit, writing

a value to it will not change it

or cause any problems

I/O PORT

DB0 DB1 DB2 DB3 DB4 DB5 DB6 DB7

ST X X X T T T T T T T T T T T T

ST = Timer status, 1 = counting down, X = Not Used

T = Timer countdown value

Trang 5

• There is no interrupt from the timer, you will need to keep examining the memory location

to see when the timer stops counting

8 Write a short 68K assembly language subroutine that will send a string of ASCII characters to

a serial port according to the following specification:

• The serial port is memory mapped as two successive byte locations at address $4000 and

$4001 The actual port for sending and receiving characters is at address $4000 and the status port is located at address $4001

• Data bit 0 ( DB0 ) of the Status Port is assigned to indicate the state of the Transmit fer When the serial device is ready to transmit the next character TBE = 1, or the signal, Transmit Buffer Empty is true When the Transmit Buffer is empty the next character may

Buf-be sent Writing a character to address $4000 starts the serial data transmission and sets TBE = 0 The next character may be sent when TBE=1

• On entry, the subroutine should save the values of any registers that it may use and restore these registers on exit

• The location of the string to print is passed into the subroutine in register A0

• The string is terminated with the null character, $FF

• The stack pointer has already been initialized, there is no need to establish a stack pointer

• Assume that this is a polling loop, there is no interrupt occurring You must continually test the state of TBE in order to know when to send the next character

9 Write a program that fills all of memory between two specified addresses with the word tern $5555 This is similar to what the block fill (BF) command does in the instruction set simulator, (ISS), but you will do it with 68K assembly language code The memory region that you will fill is $2000 to $20FF, inclusive

pat-10 Write a memory test program that will be capable of testing memory as words (16-bits at

a time) in the region from $00001000 to $0003FFFF It should test up to, and including

$0003FFFF, but not past it A memory test program works as follows:

• Fill the every memory location in the region being tested with the word data value

• Read back each memory location and compare it to the data value that you wrote

• Compare the value that you read back with the value that you wrote If they don’t agree, then you have a bad memory location

• Start testing the memory using two different test patterns: $FFFF and $AAAA

• After you complete a memory test with one of the patterns, complement the bits (change 1’s to 0’s and 0’s to 1’s) and repeat the test with the new pattern Thus, you’ll be cycling through the test a total of 4 times using these patterns

• Repeat the test one more time using the starting test pattern $0001 Use the ROL.L

instruction to move the 1 bit one position to the left each time through the memory test until you’ve run the memory test 16 times, shifting the 1 to the left each time you repeat the test

• Complement the test pattern that you just used and repeat the above bit shifting test

• Here are the particulars for the assignment:

– The program should be ORG’ed to run at memory address $00000400

– The program tests the memory from $00001000 to $0003FFFF, inclusive

– The stack pointer should be located at $000A0000

Trang 6

Programming in Assembly Language

– The starting memory test patterns are: $FFFF, $AAAA and $0001

– The test will fill all the memory region of interest with one of the test patterns Next,

it reads the pattern back and compares the value read to the value written If you write the program in a way that writes a word to memory and then reads it back immedi-ately then you are not “adhering to the specifications”

– The test is repeated for each of the two starting test patterns, their complement and the shifted bit pattern and finally, its complemented bit pattern

– If an error is detected, the address of the memory location where the error occurred, the data written and the data read back is stored in memory variables

– If more than one error occurs the program should store the total error count (number

of bad locations found) and keep only the address and data information for the last error detected

– You should allow for a count of up to 65,535 bad memory locations

• Don’t forget to initialize the stack pointer!

• The program has several loops in it What are they? How will you define the regions of memory to test? How do you know when you’ve done all of the tests? How do you know

if you wrote all of the memory locations that you need to?

• Be sure that you understand how to use the pseudo-ops EQU, ORG, CRE, DC.L, DC.B, DC.W and DS.L, DS.W, DS.B, END

• Understand the instructions JRS, RTS, LEA and the addressing modes, (An) and (An)+

• This program can be done in less than 50 instructions, but you have to know what you are trying to do The Easy68K simulator counts cycles The program that runs in the least num-ber of clock cycles, even if it has more instructions, is generally the more efficient one

• Try coding it in stages Once you’ve completed the flow chart for the program, write the assembly code for each block and test it How do you test it? Well, you can assemble it If

it assembles properly, you’ve made some progress Next, run it in the simulator and verify that it is doing what you want it to

• When you test your program, one good programming trick is to use the EQUates to change the region of memory that your testing to only a few words, not the entire space That way you can quickly walk through the code So instead of testing from $00001000 to

$0003FFFF, you test from $00001000 to $0000100A

• Check to see what happens if a memory location has bad data Change the data value in a memory location using the simulator after your program has filled it with the test pattern Did your program catch it? Did it deal with it? The Easy68K simulator has a nice memory interface that you can access through the view window

• This program is almost entirely comprised of three instructions, MOVE, Bcc, and CMP (CMPA) The addressing mode will probably be address register indirect because you’ll constantly be writing to successive memory locations and reading from successive

Trang 7

memory locations The address register is an ideal place to hold the starting address, the ending address and the address of where you currently are in memory To point to the next memory location, increment the address register’s contents You can do this explicitly by adding to it, or implicitly with the (An)+ addressing mode

• Think about how you might terminate the test when you’re using the ROL instruction You could set up a counter, and then count 32 times That would work However, look closely

at the ROL instruction Where does the “1” bit go to after it shifts out of the MSB tion? What instruction would test for that condition?

posi-The general structure of your program should be as follows:

a Comment header block: Tells what your program does

b System equates: Define your variables

c ORG statement: Your program starts here:

d Main program code: Everything except subroutines are here

e STOP $#2700 instruction: This ends the program gracefully and kicks you back into the simulator

f Subroutine header block: All subroutines should have their own header block

g Subroutine label: Each subroutine should have a label on the first instruction Otherwise you can’t get there from here

h Subroutine code: This does the work Don’t forget to keep track of what registers are doing the work and how parameters are passed

i RTS: Every subroutine has to return eventually

j Data area: All of your variables defined with the DC and DS pseudo ops are stored here

k END: The last line of the program should be END $400 This tells the assembler that the program ends here and that it should load at $400

If all else fails, review the memory test programming example in the chapter Finally, note that

if you try to run your program under the various forms of Windows you may notice strange behavior The program might seem to run very quickly if you run it in a small region of mem-ory, but then seem to die if you run it in a larger region of memory This is not your program’s fault It is a problem with Windows when it runs a console application

Windows monitors I/O activity in a console window When it doesn’t see any input or output

in the window, it severely limits the amount of CPU cycles given to the application tied to the window Thus, as soon as your program begins to take some time to run, Windows throttles it even more

There are several ways around it, depending upon your version of Windows You could try ting the ENTER key while your program is running It won’t do anything in the window, but it will fool the operating system into keeping your program alive

hit-Another trick is to open the PROPERTIES menu for the window and play with the sensitivity settings so that Windows doesn’t shut you down This worked in Win98SE at Win2000 at school

Trang 8

C H A P T E R 9

Advanced Assembly Language

Programming Concepts

Objectives

When you are finished with this lesson, you will be able to:

Program in assembly language using all addressing modes and instructions of the 68K

processor architecture;

Describe how assembly language instructions and addressing modes support high level

languages;

Disassemble memory images back to the instruction set architecture;

Describe the elements and functions of a single-board computer system.

Introduction

Now that we’re sufficiently grounded in most of the 68K programming fundamentals, let’s move deeper into subject by examining more closely some additional instructions and addressing modes The addressing modes that we’ll examine now are more obscure from an assembly language programmer’s point of view, but critically important if you’re programming for the 68K family using C or C++ as your development language of choice

Since the overwhelming majority of programmers write in C or C++, having addressing modes that support the high-level language constructs are an important consideration for computer designers The addressing modes we’ll study now allow us to implement data structures and to

write code that is position independent, or relocatable.

Relocatable code can run anywhere in the address space of the processor because there are no

absolute references to memory locations containing instruction or data Being able to be loaded anywhere in the address space is important since operating systems have to be able to manage tasks and memory in such a way that a task (program) might have to run from address $A30000 one time and $100000 another time Also, programs that are written to be position-independent tend to be more efficient in terms of memory size and speed because the destinations of jumps and fetches are determined by the contents of the registers, rather than having to be retrieved from memory as part of the instruction

Now before we attack the advanced addressing modes you might be wondering about the idea of relocatable code and absolute address references Look at the following code segment:

start LEA $4000,A0

MOVE.W D0,(A0)

Trang 9

Are we making an absolute address reference? Absolutely (sorry)! However, once we have an address in a register, even if it was initially derived from an absolute reference, we have the ability

to modify that address according to where that program is loaded in memory

Advanced Addressing Modes

Mode 5, Address Register Indirect with Displacement

A signed (positive or negative value), 16-bit displacement is added to the contents of the address register to form the effective address Thus, the effective address, (EA) = (An) +/– 16-bit displace-ment For example if <A6> = $1000, the instruction:

MOVE.L $400(A6),D0

would fetch the long word located at memory address $1400 and copy it into data register D0 This addressing mode is very important because it is used to locate local variables in C functions This is a “gotcha” in the above instruction The form of the above instruction is represented in the

Programmer’s Reference Manual as d16 (An), which would lead you to believe that the value in

the last example, $400, is a displacement value However, most assembler programs do not expect you to calculate a displacement yourself The assembler expects that you will insert a label or an absolute memory reference and it will calculate the displacement value for you Thus, the number

$400 might not be interpreted as a displacement of $400, but rather, as the memory location that you wish to calculate the displacement to Therefore, if your assembler program is giving you er-

rors, such as, displacement too large or out-of-range error, then it is likely expecting an absolute

address or label, rather than an offset

When you make a function call in C or C++, the compiler establishes a stack frame using one of

the address registers as the stack pointer The locations of the different variables are identified by their displacements from the pointer Thus, if A6 was pointing to the beginning of a stack frame for a function, foo(), the local variable being fetched is located $400 bytes from the pointer

Mode 6, Address Register Indirect with Index

The contents of the address register is added to the contents of an index register (A0 A6 or D0 D6) plus an 8-bit displacement

EA = (An) + (Xn) + d8

If <A5> = $00001000 and <D3> = $AAAA00C4, the instruction

MOVE.W $40(A5,D3.W),D4

would fetch the word contents of memory location $00001104 and copy the data into register D4

To see this, try running this code fragment in the simulator

Trang 10

Advanced Assembly Language Programming Concepts

At first, you might think that the effective address would be $AAAA1104 However, the index register for this instruction, D3, is using only the word value, not the long word value, to calculate the effective address

This addressing mode may seem very strange to you, but assume for a moment that you’ve created

an array of compound data types (structure) in C Each data type would have some fixed offset from the beginning of the structure In order to access a particular data element of a particular structure, you must index into the array using D3 and then find the particular element with the fixed offset of $40

Mode 7, Subclass 2: Program Counter with Displacement

A signed, 16-bit displacement is added to the current contents of the program counter

EA = (PC) + d16

This is an example of the general class of addressing modes known as PC relative Please don’t

mistake it to mean something related to a PC, such as a Palm Pilot® PC relative addressing is the most important addressing mode for generating relocatable code In PC relative addressing the

effective address of the operand is computed by adding the sign extended value in the extension

word to the current value of the PC The resulting address is then placed on the address lines and the data is fetched from external memory

For example, assume that the <PC> = $D7584420

$7AFE = 0111 1010 1111 1110 Sign extending the most significant bit (underlined) to 32 bits gives us 0000 0000 0000 0000 0111 1010 1111 1110

EA = $D7584420 + $00007AFE = $D758BF1E

If <PC> = $00000400 at the point in the program when the instruction MOVE.W $100(PC),D4

is executed, the word contents of memory location $500 will be fetched from memory and copied

to D4 However, if this code segment was relocated to another place in memory and run again it would still be fetched correctly, as long as the data to be fetched was located $100 bytes away from the instruction

The reason is that the current value of the program counter will be used to calculate the effective address The PC always points to the next instruction to be fetched from memory, regardless of where that program code is residing in memory As long as the effective address can be calculated

as being located a fixed distance from the current value of the PC, everything works properly.The same holds true for JUMP and JUMP TO SUBROUTINE instructions Up to now, we’ve always considered the destinations of the JMP and JSR instructions to be absolute However, consider the following two code examples:

Example: Case 1

JSR foo *assembler generates absolute address for foo

Trang 11

Example: Case 2

JSR foo(PC) *assembler generates relative address

foo {More instructions here}

RTS *Return from subroutine

In Case 1, the assembler calculates the absolute address for the label, foo When the instruction is executed, the address of the next instruction is placed on the stack and the absolute address of foo

is placed in the PC The next instruction fetched from memory is the instruction located at address,

foo

In Case 2, the assembler calculates the displacement (distance) from the current value of the PC to

foo When the instruction is executed, the address of the next instruction (current value of the PC)

is placed on the stack and the displacement is added to the current value in the PC and the sum is returned to the PC The next instruction is fetched from the location at address, foo

However, only Case 2 would allow the program (main code plus subroutines) to be moved to other location in memory without having to readjust all of the addresses

an-Mode 7, Subclass 3: Program Counter with Index

The contents of the program counter is added to the contents of an index register (A0 A6 or D0 D6) plus an 8-bit displacement Thus, EA = (PC) + (Xn) + d8

PC relative, the entire table automatically moves with the program This is very useful in compiler operations that need to address a variable with a constant offset (displacement) in a structure

68000 Instructions

Within each category of instruction there are several variants For example, the ADD instruction family consists of:

• ADD: Adds an effective address to a data register or data register to an effective address

• ADDA: Adds an effective address to an address register

• ADDI: Adds an immediate value to an effective address

• ADDQ: Adds an immediate data value from 1 to 8 to an effective address

• ADDX: Adds a data register value to a data register value and includes the value of the X bit

As you know, the effective address may be some, or all of the addressing modes we’ve studied so far Obviously, there are a number of ways to formulate an instruction to do what you want The

Trang 12

Advanced Assembly Language Programming Concepts

conclusion is that learning to program in assembly language is like learning to speak by using a dictionary Learn a few simple words first, and then broaden your vocabulary to add richness and efficiency Most algorithms can be written in many ways, but the most efficient algorithms may be hard to code without lots of programming experience First get your algorithm to run, then try to tune it up!

MOVE Instructions

MOVE (Move data): Copies data from source to destination You should know this one by now

MOVEA: (Move address): Copies data into an address register The destination is always an address

register and the data size must be word or long word Consider the formats of the MOVE and MOVEA instructions below:

Register 0 0 1 Source Mode Source Register

They look different until you realize that bits 8,7,6 are just defining a mode 1 addressing mode, which is the address register direct addressing mode So we still don’t really have a reason for a unique mnemonic The reason is that we need to have a different representation is the size of the operation Since the MOVEA instruction can only move word or long word values into an address register, we prevent the situation of inadvertently creating an illegal op-code

MOVEM(Move multiple registers): The MOVEM instruction can only be used to move registers to memory and vice versa It is most often used with the postincrementing and predecrementing addressing modes Predecrementing is used to transfer registers to a stack structure in memory and Postincrementing is used to transfer data from the memory stack back to the registers The stack pointer could be the SP register for a system stack, or one of the other address registers for a user stack frame The order of the register list is unimportant because the 68000 always writes to memory in the order A7 to A0, then D7 to D0 It always reads from memory in the order D0 to D7, then A0 to A7

Logical Instructions

AND

The AND instruction performs the bit-wise, logical AND For example, if <D0> = $3795AC5F and

<D1> = $B6D34B9D before the instruction AND.W D0,D1 is executed, then <D0> = $3795AC5F and <D1>= $B6D3081D after the instruction Note that only the lower 16-bits of each register are used in the operation To see why this is the result, let’s do the bit-wise AND in binary:

Trang 13

A simple trick to remember is this:

• Any hex digit AND’ed with F returns the digit

• Any hex digit AND’ed with 0 returns 0

ANDI

ANDI: AND immediate data Example, ANDI.B #$5A,D7

Other Logical Instructions

OR: Perform the bit-wise logical OR

ORI: OR immediate data

EOR: Perform the bit-wise logical Exclusive OR

EORI: Exclusive OR the immediate data

NOT: Perform the bit-wise logical complement of the operand

The NOT instruction takes a single operand If <D3> = $FF4567FF, then the instruction NOT

B D3 would change the data in D3 such that, <D3> = $FF456700 Note that you cannot have a

NOT Immediate instruction because NOTI.B #$67 has no meaning

Shift and Rotate Instruction

This group of instructions is rather involved in that they have different rules depending upon how many positions the bits are shifted and whether the effective address is a register or memory How-ever, shift and rotate instructions are very important as part of the overall need to manipulate bits and maneuver parts of data words to make them more accessible for arithmetic and logical operations For example, suppose that you have read four ASCII characters in from a modem The characters are $31, $41, $30, $30 Furthermore, the characters are supposed to be a 4-digit hexadecimal num-ber You consult a table of ASCII values, you’ll see that these ASCII values represent the number

$1A00, but how do we get from the four ASCII byte values to decode our number $1A00? We need a conversion algorithm

We’ll return to this problem in a moment and we’ll study an algorithm that solves it For now, let’s look at what shifting and rotating means Consider Figure 9.1

The ASL instruction moves each bit of the byte, word or long word one or more bit positions to the left Each time the bits are shifted, a 0 is inserted in the least significant bit position, DB0 The bit occupying the most significant bit position, DB7, DB15 or DB31 is moved into the carry bit and extended bit position of the condition code register, CCR The bit that was in the CCR is discard-

ed Thus, if the instruction is ASL.B #3,D0 is executed and <D0> = $AB005501 After the byte portion of D0 is shifted three times <D0> = $AB005508 The ASL instruction also has the effect of multiplying the data by 2 each time the bits are shifted to the left

Trang 14

Advanced Assembly Language Programming Concepts

The other shift and rotate

instruc-tions operate in a similar manner as

ASL and ASR Figure 9.2

summariz-es the behavior of thsummariz-ese instructions

Comparing Figures 9.1 and 9.2, we

see that some instructions seem to

exhibit identical behavior, such as

the ASL and LSL instructions, but

the ASR and LSR instructions are

slightly different

Let’s summarize some of the finer

points about using the shift and

rotate instructions

• Instructions in this

group may be byte,

word or long word

• When a data register

is involved, the

in-struction must supply

the number of times

the bits are shifted

• If the number of shifts is less than 8,use the immediate form

• If the number of shifts is greater than 8, then the number must be in another data register

• When a memory location is involved, only one bit is shifted at a time and the only word ands are allowed

oper-Finally, we’ll summarize this discussion by returning to the sample program that we discussed earlier For the purpose of this example, we’ll assume that there are four ASCII digits located in

a memory buffer called “ascii_val” The program will convert the ASCII characters to a 4-digit hexadecimal number The number will be stored back to a memory location called “hex_val” Pay particular attention to the way that the ASL instruction is used to move the bits

The example program is called Get Value It converts 4 ASCII digits stored in memory into a

4-digit long hexadecimal number

Figure 9.1: Operation of the Arithmetic Shift Left and the Arithmetic Shift Right Instructions.

LSB Byte = bit 7

Word = bit 15 Long word = bit 31

X/C

ASL - Arithmetic Shift Left

0

LSB Byte = bit 7

Word = bit 15 Long word = bit 31

X/C ASR - Arithmetic Shift Right

Lost forever

0 X/C

LSL - Logical Shift Left

Trang 15

******************************************************

* Get_value

* Converts 4 ASCII values to a 4-digit

* Input Parameters: None

* Assumptions: The buffer, ascii_val contains 4 valid ascii * characters

* in the range of 0 9, A F, or a f

******************************************************

*System equates

* Program starts here

Ngày đăng: 02/07/2014, 14:20

TỪ KHÓA LIÊN QUAN

w