ece222 lab manual f2011 uptolab1

28
University of Waterloo Electrical and Computer Engineering Department Digital Computers ECE-222 Lab manual Fall 2011 These notes are for the exclusive use of students registered in the course. Reproduction or use for any other purpose is prohibited. 1

Upload: unoriginal1

Post on 03-Sep-2014

38 views

Category:

Documents


2 download

TRANSCRIPT

University of Waterloo

Electrical and Computer Engineering Department

Digital Computers

ECE-222 Lab manual

Fall 2011

These notes are for the exclusive use of students registered in the course.

Reproduction or use for any other purpose is prohibited.

1

Table of Contents

General Information.............................................................................................................................4

People...................................................................................................................................................4

Textbook and Reference......................................................................................................................4

Lab Schedule........................................................................................................................................4

After Hours Access..............................................................................................................................4

Lab Demonstration and Reports..........................................................................................................5

Penalties...............................................................................................................................................5

Diary (Lab 2, 3, 4)................................................................................................................................5

Project Demonstration (Lab 2, 3, 4).....................................................................................................5

Final Report Contents (Lab 2, 3, 4).....................................................................................................5

Software Tools.....................................................................................................................................6

0 Lab 0: Introduction to the Coldfire Board.........................................................................................7

Objectives.............................................................................................................................................7

Prelab Reading.....................................................................................................................................7

Hardware Introduction.........................................................................................................................7

Main Lab..............................................................................................................................................8

Introduction..........................................................................................................................................8

Procedure.............................................................................................................................................9

Step 1: Power up and connect to the board..........................................................................................9

Step 2: Reset the board.........................................................................................................................9

Step 3: Loading the program into memory........................................................................................10

Step 4: Verifying the program...........................................................................................................11

Step 5: Entering variable data............................................................................................................13

Step 6: Executing the program...........................................................................................................13

Step 7: Examining the result..............................................................................................................13

Debugging Technique I - Breakpoint and Trace................................................................................14

Breakpoint..........................................................................................................................................14

Go with trace......................................................................................................................................15

Breakpoint with trace.........................................................................................................................15

Deliverables.......................................................................................................................................16

2

1 Lab 1: Hand Assembling..................................................................................................................17

Objectives...........................................................................................................................................17

Prelab reading....................................................................................................................................17

Introduction........................................................................................................................................17

Project description..............................................................................................................................17

Pseudocode.........................................................................................................................................17

The Code............................................................................................................................................18

Code Review......................................................................................................................................24

Preparation.........................................................................................................................................24

Main Lab............................................................................................................................................25

Download the existing code to the board...........................................................................................25

Manually appending the rest of the program.....................................................................................25

Run the program.................................................................................................................................25

Sign off...............................................................................................................................................26

Post Lab.............................................................................................................................................26

Deliverables.......................................................................................................................................27

Debugging Techniques II – Disassembly and Abort.........................................................................28

Disassemble.......................................................................................................................................28

Abort..................................................................................................................................................28

Programming Style............................................................................................................................28

3

General InformationPeople

Instructor Gord Agnew E5-4016 33041 gbagnew @uwaterloo.ca Instructor Hiren Patel E5-4018 38105 [email protected] Instructor Eric Praetzel E2-3357 35249 [email protected]

Textbook and Reference

• Computer Organization, 5th Edition, Hamacher, Vranesic and Zaky• 5307 Janus Coldfire Manual (PDF) • Coldfire Family Programmers Reference Manual (PDF) , MFC5200PRM/AD, Motorola

(hardcopy may be available in the laboratory)• MCF5307 Microprocessor Users Manual (PDF) , Motorola (hardcopy may be available in the

laboratory) • We are maintaining a Coldfire Mirror site with links to many Coldfire resources.

Labs are an integral part of this course. All labs must be completed in accordance with therequirements set down in this manual. Failure to do so may result in a grade of INC for thecourse.

Lab ScheduleLab Session / Experiment Report Submission Lab 0: Coldfire Introduction NoLab 1: Hand Assembly YesLab 2: Subroutines YesLab 3: Hardware Interfacing YesLab 4: Interrupt-Handling YesLaboratory Demo for Lab 2, 3, 4 No

All labs will be done in groups of two students.

After Hours Access

After hours privileges will be extended to the class via the door combination after Lab 0.Remember, this is a privilege, not a right. Loss, damage, improper use of equipment or any

4

indication of food or beverage consumption in the lab will lead to cancellation of afterhoursaccess. Please keep the lab doors closed at all times when you are in after hours.

Lab Demonstration and Reports

Penalties

• Failure to demonstrate in your scheduled lab session results in a grade of “INC”. • A "reasonable" attempt must be made of all labs or you will receive a grade of INC

(incomplete) or FTC (failure to complete).• Late submission of reports will result in penalties.

For Lab 1, please see Section 1 Deliverables..

Diary (Lab 2, 3, 4)

• Diary entries should be made throughout all debugging and testing sessions including thosesessions outside of scheduled lab slots.

• Diary entries should be brief but informative and time stamped (date and time of session).• Record entries at major milestones such as identifying and solving problems.• Make sure that your student names, and UW user IDs appear on the front of your lab diary.

Project Demonstration (Lab 2, 3, 4)

The student team will have the program ready to demonstrate when the examiner arrives and willprovide the examiner with their Submission Form plus a listing of the code that is actually running.Once the examiner is satisfied of the program's performance, they will ask questions related to thelab and should randomly check parts of the code to verify that the listing is correct. At the end ofyour Demo we will ask you to upload your listing file on UW-ACE. [Fully documented codelisting (*.LST file)]

Final Report Contents (Lab 2, 3, 4)

• Diary • Post Lab Analysis:

o How would you improve upon your code given more time?o Problems encountered during the lab.o Your approach to debugging problems.o How you might improve the lab?

• Post Lab Analysis should not exceed 2 pages • Your commented listing file must be added as an appendix. • The final report must be submitted to UW-ACE on Sunday after the lab help session. • Reports will be submitted to UW-ACE.

5

Software Tools

• GVim - Basic Linux editor in Q:\Eng\ECE\Util\gvim.bat (GVim is Graphical Vim - vim.batgives one the non-graphical version.)

• NotePad2 - Basic Windows editor under Text and Document Tools• NotePad++ - Fancy editor under Text and Document Tools• TeraTermPro - Communications program which is nicer than Hyperterminal. Start it by

running tterm in a DOS box or Q:\Eng\ECE\Util\tterm.bat

6

0 Lab 0: Introduction to the Coldfire BoardObjectives

• Hardware introduction• Familiarization with commands of the janusRom Monitor program• Introduction to machine language• Simple debugging techniques

Prelab Reading

Hardware Introduction

Figure 0.1: Coldfire board

The Coldfire board consists of a Coldfire 5307 processor (ISA-A, V3 core processor), a ROM thatstores the monitor program, SDRAM for program execution, two serial ports and a parallel port.

Two control buttons are located on the board. The Red (reset) button will initiate the boot-upsequence and clear memory contents. The Black (abort) button will terminate the program withoutmodifying the memory,return to the boot monitor and dump the contents of all registers at themoment of the button press.

The board starts up the boot code from the non-volatile Flash memory. The boot code in the Flashmemory copies the janusROM code from Flash into the SDRAM and executes the janusROMmonitor from the SDRAM.

The janusROM monitor is an interactive application that runs on the coldfire board. It providesfacilities for downloading, debugging and executing code on the coldfire board. Communicationwith the janusROM application is done using the serial communications port. The defaultapplication for serial interfacing under windows is HyperTerminal and is available under:

Start ->All Programs -> ECE -> com-1-38400.ht

7

The windows application HyperTerminal program is used for serial communication with thesettings: COM1, 38400 baud, 8 bit, no parity, 1 stop bit, 25ms line delay. These are the defaultsettings if the start menu shortcut shown above is used to launch HyperTerminal.

Pressing the red (reset) button on the board should come up with the janusROM monitor prompt asshown in Fig. within about 3-4 seconds. If the prompt does not appear then check that:

• the power supply is on,• that the board LEDs are lit,• the serial port configuration is correct, and• scroll lock is off.

If problems persist, then ask a teaching assistant or the lab instructor for assistance.

In this configuration, the PC works as a dumb terminal: it sends and receives data in the ASCIIformat across the serial port to the coldfire board.

The parallel port is used to connect to the external keypad, which you will interface to in Labs 3 and4. The eight LEDs are used to show the data on the parallel port.

Entering help or ? at the janusROM> prompt will return a list of commands available from thejanusROM prompt. Help on each command is available by entering the command name with a –hflag. For example, entering rd –h results in…

Main Lab

Introduction

In this lab, you are given a program written in machine language or op-codes. You will load the op-codes into memory using the janusROM boot monitor, and execute the program.

This simple program will do the mathematical calculation

D = A + B – C.

The long word (32 bit) data A, B, and C are manually entered into memory locations. The programwill calculate the above expression and store the result into a different memory location, D.

The main objective of this lab is to get you familiarized with the software and hardware associatedwith the lab. You do not have to understand how the program works for now.

Table 0.1and 0.2 are the program and data for the lab.

Table 0.1: Lab 0 Program

Address Op-code(s) Mnemonic Operands Comments10200000 2239 1120 0000 move.l $11200000, D1 ; Load A into Register D1

8

10200006 2439 1120 0004 move.l $11200004, D2 ; Load B into Register D21020000c d282 Add D2, D1 ; Add D2 into D11020000e 2639 1120 0008 move.l $11200008, D3 ; Load C into Register D310200014 9283 Sub D3, D1 ; Subtract D3 from D110200016 23c1 1120 000c move.l D1, $1120000C ; Save the result into D1020001c 203c 0000 0000 move.l #$0000, D0 ; Stop and return to monitor10200022 4e4f Trap #15

Table 0.2: Lab 0 Data

Address Data Comments11200000 00000001 ; Variable A11200004 00000002 ; Variable B11200008 00000001 ; Variable C1120000c -------- ; Variable D

Procedure

Step 1: Power up and connect to the board

• Make sure that the 5 Volt power supply is on.• The lab uses the program HyperTerm to communicate with the board through a serial port.• To start HyperTerm: click on Start Menu -> Programs -> ECE -> com-1-38400.ht

Note that Com-1-38400.ht is a saved profile to configure the COM port to the correctsettings.

• The prompt janusROM> should be displayed. If not, hit the <Enter> key a few times. If youstill do not see the prompt displayed check the Coldfire board for power (LEDs glowing)and that the scroll lock is off. If problems persist, ask the lab staff for assistance.

Step 2: Reset the board

Before starting any work, reset the board to clear breakpoints by pressing the Red button.

The reset will NOT clear the memory. Any program entered by a previous group will stillreside in memory and one should assume the memory is filled with garbage.

You can use the BF (block fill) command to clear the memory. For example, to set thememory between 0x10200000 and 0x10300000 to 0, you can type

janusROM>bf 10200000 10300000 0

Convention: Bold font is to indicate the user input. Regular font is the command prompt.

If the command is issued correctly, it will execute and return the next prompt.

Hint: To list all the commands, type help or ?9

When working with a memory mapped system, it is important to know the memory map.The table below shows the memory map of the Janus Coldfire board. Note that throughoutall the labs our examples store code and data in SDRAM.

Table 0.3: Memory Map

Address Range Bus BitWidth Description

Start End0x00000000 0x0FFFFFFF 8 Flash0x10000000 0x1FFFFFFF 32 SDRAM0x20000000 0x3FFFFFFF 32 Peripheral Space0x40000000 0xFFFF7FFF -0xFFFF8000 0xFFFFFFFF 32 Internal SRAM

Table 0.4: Block Fill Reference

Block Fillbf [-b|w|L] [-h] <begin><end><data>

Parameters:<begin> Starting address.<end> Ending address.<data> Data to fill with.

Options:

-h Show this message.-b|w|L Pick fill size (bytes, words, longs).

Purpose: Fill the given range of memory with the given value.

Step 3: Loading the program into memory

Now use the command MM (Memory Modify) to enter the program into the memory.

Table 0.5: Memory Modify Reference

Memory Modifymm [-b|w|L] [-h] [-d] <addr> [<data>]

Parameters:<addr> Address to (start) to modify.<data> Data to put at the given address.

Options:

-h Show this message.-d Disassemble data as entered.-b|w|L Pick move size (bytes, words, longs).

10

Purpose: Modify the values in memory. If no data is given this will bedone interactively with the user.

For example, to enter the first word of this program (See Table 1), 0x2239 at the address0x10200000, the command will be:

janusRom>mm 10200000 2239

In the previous example, if no data is provided, it will prompt the user to enter data intoconsecutive addresses.

janusRom>mm 10200000 0x10200000 := 22390x10200002 := 1120...etc

Now enter the program as listed in the Op-code column of Table 1.

JanusROM>mm 1020000010200000 := 223910200002 := 112010200004 := 000010200006 := 243910200008 := 11201020000A := 00041020000C := d2821020000E := 263910200010 := 112010200012 := 000810200014 := 928310200016 := 23c110200018 := 11201020001A := 000c1020001C := 203c1020001E := 000010200020 := 000010200022 := 4e4f10200024 := <enter key>

Note: An enter key with no data is used to terminate the input mode.

Step 4: Verifying the program

o Now that the program has been entered, we must check the correctness of the input.There are two ways to check the content of the data.o The first way is to use the peek command:

11

janusROM>peek -s 16 -c 20 10200000

Table 0.6: Peek Reference

Peekpeek [-h] [-s #] [-c #] <address>

Parameters: <address> Address to access.

Options:

-h Show this message.

-s # Set the access size in bits (8,16 or 32). Default: 32-c # Number of accesses (address auto-increments). Default: 1

o The second way is to use the di or disasm (disassemble) command. To disassembleis to translate the data in machine language back to human readable assembly language. Inthis lab, use it to verify against Table 0.1. This is an invaluable command for debugging.

Table 0.7: Disassemble reference

Disassembledisasm [-h] [-l#] <address>

Parameters:<address> Starting address.

Options:

-h Show this message.-l # Set the length, in bytes, to disassemble.

Purpose: Disassemble machine instructions into readable format.

o To check the program, type the following (one can use disasm or di):

janusROM>disasm 10200000

BUGS with di or disasm

• Branch statements are disassembled incorrectly if the jump is backwards - anextra "1" is added to the displacement. Ie 1020 00E0 may be shown as 1020 01E0.This is due to a failure of not sign extending before adding the displacement to thebase address.

12

• The DIVu (and s?) instructions are disassembled incorrectly. Only the firstand last, of 3 registers is displayed. For instance DIVU.L D1,D2:D3 is displayed asDIVU.L D1,D3 In this instruction D3 is divided by D1 and the remainder is in D2.

Step 5: Entering variable data

• Next, we are going to enter the data, or operands, into the memory. We issue mm, but thevariable data are in long words (32 bits) format (See Table 2). A flag –l is used to indicatedthe size of the input.

janusROM> mm –l 1120000011200000 := 00000005 (or 5)11200004 := 0000000811200008 := 000000021120000C := <Enter>

• As you might expect, variable data and program data are in the same format. Given anaddress, it’s impossible to tell if it’s just data or part of the program. It’s up to theprogrammer to allocate and manage the memory for each.

• Again, peek can be used to check the correctness of the input.

janusROM> peek -s 32 -c 5 11200000

Caution: The janusROM does not increment the address of the data correctly when “peeking”in long word format (32 bits). In this case, you have to read every other line0x11200000:0x00000005 (5)0x11200002: 0x00050000 (327680)0x11200004: 0x00000008 (8)0x11200006: 0x00080000 (524288)0x11200008: 0x00000002 (2)

Step 6: Executing the program

• To execute the program type:

janusROM>go 10200000

• The prompt will be returned if the program has run correctly.

Step 7: Examining the result

• The answer should be stored in Variable D (location 1120000C). To check the result, youcan use the peek command. The flag –s 32 is used to indicate the size of the data to view.

janusROM>peek –s 32 1120000C

• Before the result is moved to into the memory address of Variable D, it is also stored inregister D1. To check the values of registers, use the rd (register display) command.

13

janusROM>rd

Table 0.8: Register display reference

Register Displayrd [-h]

Parameters: none.Options: -h Show this message.Purpose: Display CPU registers.

Debugging Technique I - Breakpoint and Trace

Unlike our simplified example, most programs have more complex structures and require carefuldebugging. Poor debugging skills waste enormous amounts of time! Pay careful attention to thetechniques presented below and refer to them in subsequent labs as you encounter problems.

Breakpoint

JanusROM allows one to set breakpoints. When a breakpoint is reached, the program is halted andthe user can check the memory or registers for correctness. The user can resume execution.

In our example, let’s set breakpoints at address 0x10200006 and 0x10200016. Issue the followingcommands to add the breakpoints:

janusROM>br 10200006janusROM>br 10200016

Then, start the program with:

janusROM>go 10200000

You will notice that the execution breaks (stops) before executing the instruction at the address ofthe breakpoint. At this point you may examine the contents of the registers or other memorylocations using any of the janusROM commands. If you forget the name of a particular command, aquick help command comes in handy.

To resume from a breakpoint, cont (continue) is used.

Table 0.9: Breakpoint reference

Breakpointbreak [-h] [-d] [-l] <address>

14

Parameters:<address> Address to add/remove breakpoint from.

Options:

-h Show this message.-d Remove breakpoint from the given address instead of adding.-l List all installed breakpoints.

Purpose: Manage the breakpoints in the system.

Go with trace

Another often used debugging technique is trace. Trace executes one instruction at a time anddisplays all the registers after each instruction. Effectively, the result of an rd command is displayedafter each instruction. Trace can be used to identify problems in a program. To trace from thebeginning of a program use go with a –t trace flag:

janusROM> go –t 10200000

The response from the janusROM boot monitor is a trace>prompt indicating that you are in tracemode. Whenever the <Enter> key is hit, the monitor will execute the instruction at the currentprogram location.

To resume regular execution you can toggle the trace mode back to off using the continuecommand, cont, with a –t flag to toggle the trace mode:

janusROM>cont –t

Table 0.10: Continue reference

Continuecont [-h] [-t]

Options:

-h show this message.-t Toggle trace mode.

Purpose: Continue program execution after a breakpoint is hit orduring program tracing.

Breakpoint with trace

Trace is a powerful command. But what if you want to trace through only a certain troublesomeregion of code?

15

One trick is to use the combination of breakpoint and trace. For example, you want to trace from1020000c. You can issue the breakpoint at 1020000c and then issue cont -t once it has stopped atthat breakpoint.

Deliverables

There is nothing to be handed in for this lab. However, you should have familiarized yourself withthe hardware and software in order to complete the next lab efficiently.

16

1 Lab 1: Hand AssemblingObjectives

• Learn the difference between machine language and assembly language• Hand assemble a simple program and learn several addressing modes• Learn to download programs to the board• Become familiar with input/output subroutines using TRAP #15• HEX/DEC conversion

Prelab reading

Introduction

Assembly language is more readable and suitable for programming than machine language. In thislab, you will encounter a piece of assembly code and learn how the assembly instructions aretranslated, or assembled, into machine code.

Project description

In this lab, you will write the assembly code and “hand” assemble it into machine code. A partialprogram, which has the input and output subroutines, is assembled and ready to download to theboard. The partial program is listed below. Your assignment is to complete a small program thatreverses a character string.

The code is provided to you in an assembly listing format. The first column is the address of theinstruction, the second column is the opcode of the instruction and the third column is the assembly.It is common practice to include a comment with nearly every line of assembly in order to makereading assembly language code less of a chore.

The portion of code that you will need to modify is on the final page of the code listing. There are 8assembly instructions that are provided for you to assemble as well as a segment of code that youwill need to write and assemble. The addresses or each instruction are also to be calculated. You areto hand assemble and write the required code before coming to your first lab session. Be sure toattend the lab tutorial and read the entirety of the Lab 1 for many helpful hints.

Pseudocode

The completed program should perform the following:

• Print a message that asks for input• Call the in_string subroutine to accept input• Create a second string buffer• Copy character by character from the end of the first buffer to start of the second buffer• Call the out_string subroutine to output the result and exit to the Monitor

17

The Code

cfasm ColdFire Assembler version 2.92 (07 Jun 2005)Copyright (C) 1998-2005 Austex Software. All rights reserved. Address Opcode Mnuemonics/ Directives Operands CPU 5307 include q:\eng\ece\Coldfire\coldfire.asm * E&CE 222 Lab 1 sample program. * * Copyright 1998, University of Waterloo 10200000 PROGRAM equ $1020000010210000 OPERAND equ $1021000010220000 BUFFER equ $1022000010230000 STACK equ $10230000 0000000d CR equ $0D0000000a LF equ $0A 10210000 org OPERAND 10210000 5468 6973 2070 PROMPT dc.b "This program will print out a string in the reverseorder.",CR,LF 726f 6772 616d 2077 696c 6c20 7072 696e 7420 6f75 7420 6120 7374 7269 6e67 2069 6e20 7468 6520 7265 7665 7273 6520 6f72 6465 722e 0d0a1021003c 456e 7465 7220 dc.b "Enter string to process, followed by :",0 7374 7269 6e67 2074 6f20 7072 6f63 6573 732c 2066 6f6c 6c6f

18

7765 6420 6279 203c 456e 7465 723e 3a00 10200000 org PROGRAM * Initilization 10200000 4ff9 1023 0000 lea STACK,a7 ;initialize stack10200006 4ef9 1020 0094 jmp start ****************************************************** * * Subroutines... * ****************************************************** * Fetch a string until CR or LF and return it in * NOTE: a null termination is added * the buffer is provided by A1 * ** Stay in this until at least _1_ character is entered! * ** This prevents problems with extra CR/LF/Null characters ****************************************************** in_string1020000c 2f01 move.l D1,-(A7)1020000e 2f02 move.l D2,-(A7)10200010 2f09 move.l A1,-(A7)10200012 7400 move.l #0, D2 ; in D2 keep a count of how many characters we have10200014 4eb9 1020 0082 in_string_loop jsr in_char1020001a 4a81 tst.l D11020001c 67f6 beq in_string_loop ; ignore null characters if we get them1020001e b2bc 0000 000d cmp.l #CR,D110200024 6700 0012 beq.w exit_in_string10200028 b2bc 0000 000a cmp.l #LF,D1 ; Note LF gets caught on CF-server when prog run!1020002e 6700 0008 beq.w exit_in_string10200032 12c1 move.b D1,(A1)+10200034 5282 add.l #1, D210200036 60dc bra in_string_loop10200038 4a82 exit_in_string tst.l D21020003a 67d8 beq in_string_loop ; loop back if we have 0 characters1020003c 12bc 0000 move.b #0,(A1)

19

10200040 225f move.l (A7)+,A110200042 241f move.l (A7)+,D210200044 221f move.l (A7)+,D110200046 4e75 rts ****************************************************** * print CR, LF ****************************************************** out_crlf10200048 2f01 move.l D1,-(A7) 1020004a 323c 000d move #CR,D11020004e 4eb9 1020 0078 jsr out_char10200054 323c 000a move #LF,D110200058 4eb9 1020 0078 jsr out_char 1020005e 221f move.l (A7)+,D110200060 4e75 rts ****************************************************** * Put out a string to the terminal * A1 points to start of string; 0 marks end of string ****************************************************** out_string10200062 2f01 move.l D1,-(A7)10200064 2f09 move.l A1,-(A7) out_next_char10200066 1219 move.b (A1)+,D110200068 4eb9 1020 0078 jsr out_char1020006e 4a01 tst.b D110200070 66f4 bne out_next_char10200072 225f move.l (A7)+,A110200074 221f move.l (A7)+,D110200076 4e75 rts ****************************************************** * D1 is printed to terminal ****************************************************** out_char

20

10200078 2f00 move.l D0,-(A7)1020007a 7013 move.l #$0013,d0 ;Select function1020007c 4e4f TRAP #151020007e 201f move.l (A7)+,D010200080 4e75 rts ****************************************************** * D1 is returned from the terminal ****************************************************** 10200082 2f00 in_char move.l D0,-(A7) 10200084 7010 move.l #$0010,d010200086 4e4f TRAP #1510200088 c2bc 0000 007f and.l #$7F,d11020008e 61e8 bsr out_char ; Echo the character back to the user 10200090 201f move.l (A7)+,D010200092 4e75 rts ****************************************************** * * End of Subroutines * ****************************************************** 10200094 43f9 1021 0000 start lea PROMPT,a1 ;point to start of message1020009a 6100 ffc6 bsr.w out_string ;print message1020009e 6100 ffa8 bsr.w out_crlf ;go to next line * Get input 102000a2 43f9 1022 0000 lea BUFFER,A1 ;point to start of buffer102000a8 6100 ff62 bsr.w in_string ;read string and add a trailing NULL102000ac 6100 ff9a bsr.w out_crlf ;go to next line * Scan for end of string. 102000b0 207c 1022 0000 move.l #BUFFER,A0 ;point to start of buffer

21

bloop1 tst.b (A0)+ ;see if we found the NULL yet.

bne bloop1 ;nope, keep looping suba.l #1,A0 ;ignore the NULL movea.l #BUFFER+1024,A2 ;set up another buffer for result

Insert the necessary instructions here - approximately 5 lines. bsr.w out_string ;print the string bsr.w out_crlf ;and go to new line * Exit move.l #$0000,d0 TRAP #15 end ;optional, marking end of assembly

22

Enter thecalculatedmemory addressesand assembledopcodes here.Be sure tofollow theformat givenabove.

23

Code Review

There are one-word, two-word, or three-word instructions. Some instructions take up to three wordsto represent them. Therefore, the instruction move.l $12000000 $13000000 is invalidbecause it would require five words to assemble into machine language (opcodes). It is importantthat you recognize why this is. If you do not, then this might be a good time to get acquainted withyour TAs.

Take a look at address 0x10210000 at the beginning of the program. The whole chunk of opcodesoriginating at this address is the hexadecimal representation of the prompt messages encoded inASCII. In Lab 2, you will add prompt messages this way. Note that all strings are terminated witha 0. The 0 at the end of each string represents the end of a string and is called the NULL character.

The definition of the acronym ASCII is a common trivia question. Do you know what it means? Ifnot, now might be a good time to visit Google/Wikipedia or your favorite search engine of the day.The ASCII table will be a useful reference for the rest of the labs.

CR (carriage return) and LF (line feed) are ASCII characters that are used by terminal emulatorsoftware to produce a new line. The CR character returns the cursor to the beginning of the linewhile LF moves the cursor to the next line. Carriage return and line feed parlance comes from thedays of the typewriter.

There are 3 actions associated with TRAP #15 routines. Once the TRAP #15 instruction is executedit looks at the content of Register D0 (function number) to determine the action. The TRAPinstruction is what is called a software interrupt. You will learn more about interrupts in Lab 4. Fornow, think of the TRAP #15 instruction as a way for your program to communicate with thejanusROM boot code. The boot code provides three commands that can be accessed via the TRAP#15 software interrupt. These are:

• IN CHAR:When TRAP #15 is executed and D0 has a value of $10, the routine waits for a singlecharacter input from the serial port and stores it to the lower byte of D1. (See Section 1The Code at address 10200074)

• OUT CHAR:When D0 has a value of $13, the routine outputs the lower byte of D1 to the serial port.The serial output will be received and displayed by your terminal program in ASCIIformat. (See Section 1Code address 1020006a.)

• RETURN TO MONITOR:When D0 has a value of $0, TRAP #15 returns to the boot monitor. You can use thismethod to mark the end of the program and exit gracefully. (See 1.1.4 the last two linesof the code)

Preparation

You should write and hand-assemble the required code before coming into the lab. In one labsession there is not enough time for hand-assembling and debugging.

24

Also remember to print the Grade Form for the lab.

Main Lab

Download the existing code to the board

In Lab 0, the program is entered using opcodes word by word manually. For a larger program, thisis impractical. JanusROM provides a means to download the program into the RAM via the serialport.

There are two ways to download the data. Motorola S-Records, or plain monitor commands.

The Motorola S-record format is produced by many assemblers and compilers. S-record formatencapsulates the opcodes with the addresses and checksums. This S-19, or object file, can bedownloaded as explained in Lab 2.

The second form of downloading, using monitor commands, will be used here. The first part of theprogram has been assembled for you and is ready for download. The OPCODES.TXT file on UW-ACE has the data that you can download. Save that file to a folder on your N: drive.

If you examine that file, you will see it is just a listing of JanusROM commands. TheOPCODES.TXT file effectively uses the memory modify (mm) janusROM command to enter theprogram. If we send the OPCODES.TXT file with the terminal program, janusROM cannot tell itwas not typed by hand.

To download, make sure your system is sitting at the janusROM prompt. Then, in HyperTerminal‘smenu bar, go to Transfer or Send Text File . Select to view all file types and then choose theOPCODES.TXT file that you previously saved on your N: drive.

Be sure to do all your work on the N: and not on the Desktop. The files stored on the Desktopare part of your roaming profile and are actually saved on the local machine. In the event of anetwork outage or an improper log-out, these files will be lost. The N:, on the other hand, is amapped network drive. Files will be updated on the network drive with every save.

Manually appending the rest of the program

Now use mm (Memory Modify) to enter the rest of your hand-assembled program. You may alsoadd your hand assembled and newly written opcodes to the previous file and transfer again asdescribed above.

Run the program

Execute the program and check for correctness. If there are any errors, use breakpoints, traces anddisasm to debug your program (See). See Section 1 for more debugging techniques.

25

Sign off

When the program is completely functional, demonstrate the program to a TA to get your namechecked off. Questions about the hand assembly methods and/or the functionality of the givenprogram will be asked.

Post Lab

All execution is done in binary. The input and output of a program are typically displayed in, ASCIIencoded, decimal format for readability. As a result, we need to have subroutines to convert databetween ASCII encoded decimal form and binary form. Note a "0x" prefix means a hex. number.

To convert a digit into an ASCII byte, simply add 0x30 to it. For example, the decimal input string"3210" will be stored as 0x33 0x32 0x31 0x30 0x00. A NULL (0x00) is added at the end of eachstring as an end of string delimiter. The in_string subroutine given in the code segment aboveappends a NULL character to the end. Similarly, the out_string routine requires a NULL characterto delimit the end of a string.

Therefore, to convert the ASCII character input string "3210" to binary form represented as 0xC8Ain hex you have to subtract 0x30 from each digit and apply Horner’s equation as shown below.

In decimal form: 3210 = (((3)*10+2)*10+1)*10+0

In hexadecimal form: 3210 = (((3)*0xA+2)*0xA+1)*0xA+0

The Coldfire architecture is byte addressable. The memory contents of the address BUFFERcontaining the character string “3210” will be:

Table 1.11: BUFFER contents byte-addressed

Table 1.12: BUFFER contents word-addressed

Table 1.13: BUFFER contents longword-addressed

26

Address ContentsBUFFER 0x33BUFFER+1 0x32BUFFER+2 0x31BUFFER+3 0x30BUFFER+4 0x00

Address ContentsBUFFER 0x3332BUFFER+2 0x3130BUFFER+4 0x0000

Address ContentsBUFFER 0x33323130BUFFER+4 0x00000000

Conversion from ASCII to a number stored in memory is accomplished using byte addressing.

In order to convert a binary number to a decimal number, you need to divide the number severaltimes and collect the remainders. Assume an unsigned input of 16 bits in register D1. The rangewill be from 0 (0x0000) to 65,535 (0xFFFF). For example, converting 1345 into an ASCII numberwould be accomplished by successively dividing by 10 and collecting the remainders as:

1345/10 = 134:5134/10 = 13:413/10 = 1:31/10 = 0:1

where we have used the notation in the form of [quotient]:[remainder].

If we collect the remainders and add 0x30 we will have 0x35,0x34,0x33,0x31 which is a reversedversion of the desired result. All that remains is to reverse the character string, append the NULLcharacter and we’re done.

Your assignment for the post lab is to write your own BIN2DEC (register word to ASCII) andDEC2BIN (ASCII to register word) subroutines in assembly language. You are not required tohand-assemble them. The DEC2BIN and BIN2DEC routines you prepare as part of this post labwill be used in subsequent labs.

DEC2BIN

• Assume that the start address of the stored input string is loaded into A1• Convert the A1 decimal ASCII string into a binary number (Range 0x0000 to 0xFFFF).• Error checking is not required.

BIN2DEC

• Assume a word sized binary number is stored in D1• Convert the binary number into ASCII decimal string.• Assume the converted characters are written in the address pointed to by A2, and onwards.• Error checking is not required.

Deliverables

Submit your final report on UW-ACE, it should include:

• A copy of your hand assembled code from the main portion of the lab• DEC2BIN and BIN2DEC subroutines in assembly language from the post lab portion of the

lab

27

Debugging Techniques II – Disassembly and Abort

Disassemble

How do you know if you hand assembled the program correctly? The quickest way, without doublechecking your calculations, is to use the disasm (or di)command in janusROM. This is one of themost powerful debugging tools for hand assembly. Refer to Lab 0 for the syntax or use disasm –h

Abort

When a program is executed, it may lock in an infinite loop. The Abort button (black) on the boardwill halt the program and display the address before the halt. This enables you to debug the problemby identifying the portion of your code where execution is stuck. However, this does not alwayswork and you may have to resort to the red reset button.

Programming Style

The Lab 1 Sample Program includes two ORG or origin statements. The ORG statement is used inthis case to separate the data from the executable program code. One method of constructing anassembly program is to simply have one origin (the start of memory, for example) and let theassembler place everything in memory. A drawback of this technique is that you do not know whereanything is in memory until you’ve assembled the program and looked at the assembly listing tolocate the memory addresses of your global variables, stacks, and subroutines.

A better technique is to manually arrange the memory yourself and to use divide the memory by inlogical partitions based on function. The most basic way of dividing memory is to separate thecode, data and stack. If you notice that execution of your program has entered the code segment orthat the stack has grown to into your code or data you will instantly recognize the fault by checkingthe addresses of the program counter (PC) or stack pointer (A7).

Pay special attention to the location of the stack relative to your program and data code. A commonerror is for the stack to overflow into program or data segments. Ensure that you load anappropriate address into the stack pointer A7 as is done on the second page of the code listingprovided for Lab 1 using the assembly instruction lea STACK,A7.

In order to make full use of the assembler you should familiarize yourself with the common pseudoops or special assembly commands that are available to you. Pseudo ops are not documented in theprogrammer’s manual because they tend to be specific to each assembler. The assemblerdocumentation is available on the course website. Consult the assembler documentation for detaileddescriptions of the most common pseudo ops: ORG (ORiGin), DS (Define Storage), DC (DefineConstant), EQU (EQUate) and ALIGN.

28