Vous êtes sur la page 1sur 12

CIS*2030 - Lab#2

Assembling Programs and Basic Data


Movement and Arithmetic Instructions
Objectives
The purpose of this lab is two-fold: (a) to demonstrate how a pre-written 68000 assembly
language program can be assembled, downloaded into RAM, and run on the 68KMB, and (b) to
commence exploration of the 68000s Instruction Set Architecture (ISA) by focusing on some of
the more common data movement and arithmetic instructions.
Upon completion of this lab students will be able to:
Assemble a pre-written assembly-language program and convert the resulting machinelanguage program into the Motorola S-Record format,
Use TeraTerms file-transmit command to download Motorola S-Records directly into the
68KMBs memory,
Understand how to use the MON68Ks trace mechanism to run through programs on the
68KMB one instruction at a time,
Understand how the basic data movement and arithmetic instructions (MOVE, SWAP,
ADD, SUB, MUL, DIV, and EXT) function with different size data, and how they affect the
flags in the 68000s condition-code register following execution.
During this lab you will make use of the following 68000 assembly-language programs which
are available from course website:

hello.x68
L2_1.x68
L2_2.x68
L2_3.x68
L2_4.x68
L2_5.x68
L2_6.x68

Part A: How to Assemble and Download a Program to the 68KMB


You will recall that in Lab 1 we manually entered a simple machine-language program into the
memory of the 68KMB before running the program directly on the Motorola 68000. This
turned out to be a very tedious process despite the fact that the program consisted of just two
instructions! In Lab 2, we will take a much more efficient approach to running programs on the
68KMB. In particular, we will learn how to take pre-written assembly-language programs and
automatically convert them into machine-language programs using a program called an
assembler. We will then learn how to convert the (binary) machine-language program produced
by the assembler into Motorola S-Records. The previous S-Records are a common format for
transferring binary data between devices. The binary data are encoded as ASCII characters
which can then be transferred between two devices using a simple serial protocol, like the one
that exists between the SunRay and the 68KMB.

To illustrate the previous process, we will work with the infamous Hello World! program shown
below in Fig. 1.

*hello.x68 displays the message Hello world! on the SunRay console


PROGRAM

MESSAGE

ORG
LEA
TRAP
TRAP

$8000
MESSAGE,A1
#2
#14

DC.B
DC.B
END

Hello World!
$0D,$0A,0
PROGRAM

;load message into address register A1


;use system call to display message
;use system call to return to 68KMB
;place ASCII string in memory at MESSAGE
;carriage return, line feed, null

Figure 1: How to print Hello World!


As you can see, displaying the string Hello World! on the screen is not as simple as in
other languages, like C. In particular, the ORG directive tells the assembler that the program
instructions are to start in memory at hexadecimal address 8000. (Remember from class that
a $ tells the assembler to interpret the numeric value that follows as a hexadecimal value.)
The LEA instruction stands for Load Effective Address and loads the address of the item
labeled MESSAGE into address register A1. In other words, it causes A1 to be initialized
with the starting (memory) address of the string Hello World!. TRAP instructions are
system calls to the MON68K program that cause the monitor program to perform some form
of processing on behalf of the caller. TRAP 2 causes the null-terminated string pointed to by
A1 to be displayed (actually sent to the SunRay using the serial connection) on the screen.
TRAP 14, on the other hand, is used to terminate the program and pass control back to the
monitor program. As far as data is concerned, the DC directive reserves memory for the
items that follow it (in this case our string followed by carriage return and linefeed characters.)
A NULL is also included at the end because the TRAP 2 system call requires the string to
be displayed to be terminated with 0. The assembler directive END is needed to tell the
assembler where the program ends. At this point, dont worry too much about the syntax and
semantics of the previous program. Things will become clearer as the course progresses.
Altogether, seven steps are required to run the previous program on the 68KMB.
1. After logging in, create the directory structure H:\2030\L2 under the H: directory.
Download the hello.x68 file from the course website and save it as hello.x68 under the H:
directory. Note: The text in the file should be left justified; that is, it should start at
column 0. Also, be sure to explicitly type the .x68 extension when saving!
2.

To assemble the program; that is, to convert the assembly-language program to machinelanguage, first open a DOSBox window from Start Menu>All Programs>DOSBox, then
change into the directory H:\2030\L2 and enter the following command:
H:\2030\L2>asm68 hello
The previous command causes the assembler (asm68) to take the ASCII encoded source file
(hello.x68) and convert it into a binary (or object) file (hello.bin) that contains the binary code
produced by the assembler. Note: the assembler will only produce a binary file if there are
no syntax errors in the original source file.

3. The 68KMB expects the machine-language program to be encoded using Motorola SRecords before it is downloaded. To convert the binary file created in step 2 (hello.bin) to
Motorola S-Records (hello.txt) open a Windows Command Prompt from Start
Menu>All Programs>Accessories, change into directory H:\2030\L2 and
enter the
following command:
H:\2030\L2>srec hello
This will cause the file hello.txt to be created. This is the file that will be downloaded to
the 68KMB.

4. Following the procedure described in Lab 1 establish communication between the SunRay
and the 68KMB. Recall, you will need to run TeraTerm with the correct parameters, connect
the SunRay to the 68KMB by plugging the serial cable into the J3 socket on the 68KMB,
power up 68KMB, and press the RESET button on the board until the following prompt
appears on the TeraTerm terminal window:
MON68K
V4.4>
NOTE: If MON68K becomes unresponsive after a period of being idle, press the
MONITOR button, then press the RESET button to reboot the board.
5. Now that the board is communicating with the SunRay, we can download the program (from
step 3) to the 68KMB. To transfer a file, first enter the monitor command l (load). This
command will prepare the 68KMB to load an S-record file from the SunRay into the
68KMBs memory.
6. To transfer the file from the SunRay to the 68KMB, use the mouse to select the Send file
command from the File Menu. An Open File Dialog will appear. Select hello.txt
(generated in step 3) under the H:\2030\L2 directory. Click OK. (You can also drag and
drop the file into TeraTerm from Windows Explorer.)
7. When an entry message entry: 008000 appears on the screen, press ctrl+Z. The file
transfer is now completed. To run the program enter go 8000 at the monitor prompt. The
message Hello world should be displayed on your screen. If the message does not appear,
check to make sure the serial cable is connected to your computer, or ask the TA for help.
NOTE:
If Hello world0 is displayed go to the Setup menu and click Terminal. Change the Receive
value from CR to CR+LF, then click OK.
Below, you will find a summary of the previous steps. You will want to keep this summary
handy, at least until you become comfortable performing all of the steps

Assembling and Running Code Cheat Sheet


1.
2.
3.
4.

Save file as filename.x68


In DOSBox*: asm68 filename (no extension)
In Windows terminal*: srec filename (no extension)
In TeraTerm:
L
File -> Send File
Select .txt file to load
ctrl+Z
GO <address>
make sure that you are in the folder that contains the .x68 file

Part B: The MOVE Instruction


Now that we know how to assemble and run an assembly-language program on the 68KMB, we
will use this knowledge as we explore the mechanics of the 68000s MOVE instruction. This
instruction is by far the most commonly used instruction in the processors instruction set, and
accounts for roughly two-thirds of all instructions used in a typical assembly-language program.
Review the MOVE instruction described on page 316 of your textbook along with the
examples given in Section 2.2.1. Below you will find a program that performs four consecutive
MOVE operations involving data registers before terminating. Download the program (called
L2_1.x68) from the course website and load it into the 68000s memory using the steps outlined
in Part A.
*L2_1.x68: Performs a series of moves involving data registers
PROGRAM

ORG
MOVE.B
MOVE.W
MOVE.L
MOVE
TRAP
END

$8000
D0,D1
D0,D2
D0,D3
D0,D4
#14
PROGRAM

Figure 2: Examples of MOVE instructions.


1. Recall that the ORG assembler directive places the instructions (or data) that follow it in the
program into memory at the specified address; in this case, the instructions will be placed in
memory starting at hexadecimal address 8000. Try running the monitor command DI 8000 5
to confirm that your program now resides in memory at hexadecimal address 8000. (Notice
that the ORG and END directives have disappeared and do not appear in memory! We will
discuss why this is in more detail in Lab 3.)
2. Before we can run the program, we will need to initialize D0 with some data. Use the PERIOD
command that you learned in Lab 1 to load the hexadecimal value 56781234 into data register
zero.
4

3. Use the register dump command to help fill in the first column (Before Move) of the table
below with the current 32-bit values (expressed in hexadecimal) contained in D0 through
D3.
Before Move
After Move
D0
D1
D2
D3
D4
4. Now run the program by entering the monitor command GO 8000. Once the program
completes execution, fill in the second column (After Move) of the table above with the new
data-register values expressed in hexadecimal.
a) Why do data registers D1 through D3 hold different values, even though they are a
copy of D0? Be precise.

b) Do data registers D2 and D4 contain the same or different values? Explain.

Rather than using the MON68Ks PERIOD command to initialize data registers prior to a
program running, it is possible to use the MOVE instruction to initialize registers at runtime. (In
fact, this is one of the most common uses of MOVE instructions.) This can be done by using the
MOVE instruction in combination with the immediate (or constant) address mode. Recall from
class that an immediate (constant) value can be specified in an instruction that supports immediate
addressing by placing a # before the numeric value. Numeric values can be specified either in
hexadecimal ($), binary (%), or decimal (nothing). Figure 3 shows how the previous program
(L2_1.x68) can be adapted so that D0 is now initialized using a MOVE instruction.
*L2_2.x68: Performs a series of moves involving data registers
PROGRAM

ORG
MOVE.L
MOVE.B
MOVE.W
MOVE.L
MOVE
TRAP
END

$8000
#$56781234,D0
D0,D1
D0,D2
D0,D3
D0,D4
#14
PROGRAM

Figure 3: Initializing data registers using immediate addressing.


5

5. Download the program (called L2_2.x68) from the course website and load it into the
68000s memory using the steps outlined in Part A. Then run the program and ensure that it
produces the same output as the previous program (L2_1.x68).
a) Below, rewrite the MOVE.L #$56781234,D0 instruction so that the source operand
(numeric value) is specified in binary rather than hexadecimal:

Part C: The ADD Instruction


Next to the MOVE instruction, the next most frequently used instructions are the basic add and
subtract instructions. Review the ADD instruction described on page 267 of your textbook.
Below you will find a program that adds four 8-bit numbers $21, $57, $B9 and $7D using byte
addition. Download the program (called L2_3.x68) from the course website and load it into the
68000s memory using the steps outlined in Part A.

*L2_3.x68: Adds four 8-bit numbers using byte addition


PROGRAM

ORG
MOVE.L
ADD.B
ADD.B
ADD.B
TRAP
END

$8000
#$21,D0
#$57,D0
#$B9,D0
#$7D,D0
#14
PROGRAM

;D0
;D0
;D0
;D0

=
=
=
=

$21
$21 + $57
$21 + $57 + $B9
$21 + $57 + $B9 + $7D

Figure 4: Program to sum four bytes.


1. Run the program by entering the monitor command GO 8000.
a) What is the final 32-bit hexadecimal result in D0?

b) What is the expected value in D0?


2. So, what went wrong? Was the algorithm faulty or have we failed to notice a subtlety? To
better understand what is happening in the previous program we will re-run the program this
time making use of the MON68Ks trace facility. The trace command (TR) is one of the
most useful monitor commands as it permits the execution of a single instruction at a time.
After an instruction has been executed, the contents of all registers are displayed on the screen.
Entering further carriage returns will step through the program instruction by instruction.
Thus, you can use the trace command to walk through a program instruction by instruction
10
10

until you find the cause of an error. Read about the how to use the TR command in
Appendix E, page 404 of your textbook.
Before using the TR command you must set the Program Counter (PC) to the address of the
first instruction in your program. This can be done using the PERIOD command:
V4.4>.PC 8000
In our case, the PC is set to 8000, as this is the hexadecimal address of the first instruction in
the current program (Fig. 4).
Once you have initialized the PC to hexadecimal 8000, enter the program can be traced by
entering the monitor command:
V4.4>.TR 1

Below, is the beginning output of the trace. (Note that the values in the registers (below)
may differ from what you might see on your own board.)
V4.4>.PC 8000
V4.4>TR 1
PC=00008002 SR=A700=TS7.. US=00008800 SS=0000BFD2
D0=00000021 D1=00000000 D2=00000000 D3=00000000
D4=00000000 D5=00000000 D6=00000000 D7=00000000
A0=00000000 A1=00000000 A2=00000000 A3=00000000
A4=00000000 A5=00000000 A6=00000000 A7=0000BFD2
---- 008002
06000057
ADD.B #87,D0
Notice that the program counter does not contain the hexadecimal address 8000, rather the
hexadecimal address 8002. This is because the trace command has already executed the first
instruction (MOVE.L #$21,D0) in the program, and now the PC is pointing at the next
program instruction in memory to be executed (located at hexadecimal address 8002). If you look
at the bottom line of the output, it shows both the hexadecimal memory address 008002 and the
name (or mnemonic) of the next instruction to be executed (ADD.B #87,D0).
Another very important piece of information that is provided by the trace facility is the status
of the Condition-Code Register (CCR) flags. As discussed in class, after the processor executes
an instruction the flags in the condition-code register are updated to reflect the nature of the result.
The CCR flags can be found in the lower byte of the Status Register (SR). If a particular bit in
the CCR is set to one, the first letter of the corresponding flag will be visible. For example,
consider the partial trace output given below. The N (negative) flag is currently set to 1 since the
letter N is showing. This implies that the result of the previous operation was, in fact, negative.
V4.4>.PC 9030
V4.4>TR 1
11
11

PC=00009036 SR=A708=TS7.N US=00008800 SS=0000BFD2


D0=00000021 D1=00000000 D2=00000000 D3=00000000

Returning to the program at hand, trace through the entire program (L2_3.x68) from start
to finish. (Remember, pressing the ENTER key at this point will step to the next instruction in
your program. If you want to stop the trace, type in . and hit ENTER.) After each instruction
executes, record the 32-bit hexadecimal values contained in data register D0 in the table below,
along with the value of the CCR flags.
Current Instruction

Data Register
D0

Condition Code Register Flags


N

MOVE.L #$21,D0
ADD.B #$57,D0
ADD.B #$B9,D0
ADD.B #$7D,D0

a) Why does the first addition instruction (ADD.B #$57,D0) clear the C flag, but the
second addition instruction (ADD.B #$B9,D0) cause the C flag to be set?

b) Explain why the final value in D0 does not match the expected value?

c) How would you fix the previous program so that it computed the sum of the four bytes
correctly?

12
12

Part D: The SUB Instruction


Review the SUB instruction described on page 352 of your textbook. Below you will find a
program that performs a series of additions and subtractions on 8-bit numbers. Download the
program (called L2_4.x68) from the course website and load it into the 68000s memory using
the steps outlined in Part A.

*L2_4.x68: Adds and subtracts 8-bit numbers


PROGRAM

ORG
MOVE.B
ADD.B
ADD.B
SUB.B
SUB.B
TRAP
END

$8000
#$77,D0
#3,D0
#9,D0
#1,D0
#4,D0
#14
PROGRAM

Figure 4: Program to sum and subtract bytes.

Perform a single-step trace of the program. After each instruction executes, write down the
resulting 32-bit value (expressed in hexadecimal) in register D0 and the current status of each
flag in the CCR. Remember to initialize the PC to 8000 before issuing the TR command.

Instructions

Value in D0

CCR Flags
Z
V

MOVE.B #$77,D0
ADD.B

#3,D0

ADD.B

#9,D0

SUB.B

#1,D0

SUB.B

#4,D0

Recall from class that an operand may be interpreted as either signed or unsigned. Lets assume
that the lower byte in D0 is a signed integer. This means that the most-significant bit (MSB)
should be interpreted as a sign bit, where 0 means positive and 1 means negative. Using the
information collected in the previous table, after each instruction executes identify if a sign
change does or does not occur and explain why by making reference to the flags in the CCR.

13
13

Now lets assume that the lower byte in D0 is an unsigned integer. Is a borrow required when
performing the last subtraction instruction (SUB.B #4,D0)? Your explanation should make
reference to the appropriate flag in the CCR.

Part E: Multiplication, Division and Related Instructions


The signed and unsigned multiply and divide instructions are complicated by the fact that the
results and the operands are mixed data sizes. The multiply instructions take word operands and
produce long word results. The divide instructions expect the dividend and the divisor to be a
long word and word, respectively. Also, the result has two parts: a quotient and a remainder
both of which are words. (Before proceeding carefully review signed and unsigned
multiplication and division on pages 84, 299, 301, 327 and 328 of your textbox.)
The EXT (sign extend) instruction is useful for multiply and divide operations, since in many
cases the operands must first be sign extended to the correct data types (Word or Longword).
Recall from class that the EXT instruction can be used to sign-extend a signed byte to a signed
word, or a signed word to a signed long word. (Remember, only you know if the bytes/words
are signed or unsigned!) A byte cannot be sign extended to a long word in a single instruction; it
requires two. (Before proceeding carefully review the EXT instruction on pages 85 and 308
of your text book.)
`

The SWAP instruction is used to swap the upper and lower words of a given register. This
comes in handy when you want to examine the remainder of a division result. (Before proceeding
carefully review the SWAP instruction on pages 86 and 358 of your textbook.)
The CLR instruction is used to set to zero the contents of a register or memory location.
(Before proceeding carefully review the CLR instruction on page 292 of your textbook.)
Download the following program (called L2_5.x68) from the course website and load it into the
68000s memory using the steps outlined in Part A.

14
14

* L2_5.x68 A program that uses MULS, DIVS, EXT and SWAP


ORG
PROGRAM

MOVE.W
MOVE.B
MULS
EXT.W
DIVS
CLR.L
MOVE.W
MOVE.L
CLR.W
SWAP
TRAP
END

$8000
#9,D2
#-2,D3
D2,D2
D3
D3,D2
D1
D2,D1
D2,D0
D0
D0
#14
PROGRAM

Figure 5: Program illustrates use of MULS, DIVS, EXT, and SWAP instructions.
Perform a single-step trace of the program. After each instruction executes, write down the
resulting 32-bit value (expressed in hexadecimal) in the registers. Remember to initialize the PC
to 8000 before issuing the TR command.

Instructions

Value in D0

Value in D1

MOVE.W #9,D2
MOVE.B #-2,D3
MULS D2,D2
EXT.W D3
DIVS.W D3,D2
CLR.L D1
MOVE.W D2,D1
MOVE.L D2,D0
CLR.W D0
SWAP D0
a) What does this program do? Be concise!

15
15

Value in D2

Value in D3

Now it is your turn. Using the formula below as a guide, write a 68000 assembly-language
program to convert Celsius (C) to Fahrenheit (F):
F = (C 9)/5 + 32
where C is a 16-bit signed value stored in data register D0, and constants 5, 9 and 32 are all 16bit values that are specified using immediate addressing; that is, the literal values appear in the
instructions that use them preceded by a # sign. The result F should be stored in data register
D1.
A template for creating your program can be found on the course website with the name
L2_6.x68. This template is shown in Fig. 6. Make sure to put your code where instructed.
*L2_6.x68: Template
ORG $8000
*Put Code Here
TRAP
END

#14
$8000

Figure 6: Template for creating program.

As a result of completing this lab you should have a working knowledge of how to assemble,
download into RAM, and run a program on the 68KMB; you should also have a basic
understanding of the 68000s main data movement and arithmetic instructions; and how to trace
through a program instruction by instruction.

Looking ahead to Lab 3


Next time we will take a closer look at assembly-language programming by focusing on a subset
of the main assembler directives. These directives can be used by a programmer to tell the
assembler something that it needs to know in order to carry out the assembly process the way
that the programmer wants it carried out. These directives are very important, and will be used
throughout the remainder of the course.
Remember: Please see me (Charlie) or one of the TAs right away if you have any difficulties or
questions pertaining to this lab.

16
16

Vous aimerez peut-être aussi