Sei sulla pagina 1di 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.

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

General Information
People
Instructor Instructor Lab Instructor Gord Agnew Hiren Patel Eric Praetzel E5-4016 E5-4018 E2-3357 33041 38105 35249 gbagnew@uwaterloo.ca hdpatel@uwaterloo.ca praetzel@uwaterloo.ca

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 the requirements set down in this manual. Failure to do so may result in a grade of INC for the course.

Lab Schedule
Lab Session / Experiment Lab 0: Coldfire Introduction Lab 1: Hand Assembly Lab 2: Subroutines Lab 3: Hardware Interfacing Lab 4: Interrupt-Handling Laboratory Demo for Lab 2, 3, 4 Report Submission No Yes Yes Yes Yes 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 afterhours access. 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 those sessions 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 will provide 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 the lab and should randomly check parts of the code to verify that the listing is correct. At the end of your Demo we will ask you to upload your listing file on UW-ACE. [Fully documented code listing (*.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.bat gives 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

Lab 0: Introduction to the Coldfire Board


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

Objectives

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 that stores 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-up sequence and clear memory contents. The Black (abort) button will terminate the program without modifying the memory,return to the boot monitor and dump the contents of all registers at the moment of the button press. The board starts up the boot code from the non-volatile Flash memory. The boot code in the Flash memory copies the janusROM code from Flash into the SDRAM and executes the janusROM monitor from the SDRAM. The janusROM monitor is an interactive application that runs on the coldfire board. It provides facilities for downloading, debugging and executing code on the coldfire board. Communication with the janusROM application is done using the serial communications port. The default application 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 the settings: COM1, 38400 baud, 8 bit, no parity, 1 stop bit, 25ms line delay. These are the default settings 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 as shown 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 ASCII format 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 and 4. 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 the janusROM prompt. Help on each command is available by entering the command name with a h flag. 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 opcodes 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 program will 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 associated with 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 Comments 10200000 2239 1120 0000 move.l $11200000, D1 ; Load A into Register D1 8

10200006 1020000c 1020000e 10200014 10200016 1020001c 10200022

2439 1120 0004 d282 2639 1120 0008 9283 23c1 1120 000c 203c 0000 0000 4e4f

move.l Add move.l Sub move.l move.l Trap

$11200004, D2 D2, D1 $11200008, D3 D3, D1 D1, $1120000C #$0000, D0 #15

; Load B into Register D2 ; Add D2 into D1 ; Load C into Register D3 ; Subtract D3 from D1 ; Save the result into D ; Stop and return to monitor

Table 0.2: Lab 0 Data Address 11200000 11200004 11200008 1120000c Data 00000001 00000002 00000001 -------Comments ; Variable A ; Variable B ; Variable C ; 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 correct settings. The prompt janusROM> should be displayed. If not, hit the <Enter> key a few times. If you still 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 still reside 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 the memory 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 throughout all the labs our examples store code and data in SDRAM. Table 0.3: Memory Map
Address Range Start End 0x00000000 0x0FFFFFFF 0x10000000 0x1FFFFFFF 0x20000000 0x3FFFFFFF 0x40000000 0xFFFF7FFF 0xFFFF8000 0xFFFFFFFF Bus Bit Width 8 32 32 32 Description Flash SDRAM Peripheral Space Internal SRAM

Table 0.4: Block Fill Reference Block Fill


bf [-b|w|L] [-h] <begin><end><data> Parameters: <begin> Starting address. <end> Ending address. <data> Data to fill with. Options: -h -b|w|L Show this message. 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 Modify
mm [-b|w|L] [-h] [-d] <addr> [<data>]
Parameters: <addr> Address to (start) to modify. <data> Data to put at the given address. Options: -h -d -b|w|L Show this message. Disassemble data as entered. Pick move size (bytes, words, longs).

10

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

For example, to enter the first word of this program (See Table 1), 0x2239 at the address 0x10200000, 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 into consecutive addresses. janusRom>mm 10200000 0x10200000 := 2239 0x10200002 := 1120 ...etc Now enter the program as listed in the Op-code column of Table 1. JanusROM>mm 10200000 10200000 := 2239 10200002 := 1120 10200004 := 0000 10200006 := 2439 10200008 := 1120 1020000A := 0004 1020000C := d282 1020000E := 2639 10200010 := 1120 10200012 := 0008 10200014 := 9283 10200016 := 23c1 10200018 := 1120 1020001A := 000c 1020001C := 203c 1020001E := 0000 10200020 := 0000 10200022 := 4e4f 10200024 := <enter key> Note: An enter key with no data is used to terminate the input mode. Step 4: Verifying the program 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:
o

11

janusROM>peek -s 16 -c 20 10200000 Table 0.6: Peek Reference Peek


peek [-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 disassemble is to translate the data in machine language back to human readable assembly language. In this lab, use it to verify against Table 0.1. This is an invaluable command for debugging. Table 0.7: Disassemble reference

Disassemble
disasm [-h] [-l#] <address> Parameters: <address> Starting address. Options: -h -l # Show this message. Set the length, in bytes, to disassemble.

Purpose: Disassemble machine instructions into readable format.

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 - an extra "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 the base address.

12

The DIVu (and s?) instructions are disassembled incorrectly. Only the first and last, of 3 registers is displayed. For instance DIVU.L D1,D2:D3 is displayed as DIVU.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 the variable data are in long words (32 bits) format (See Table 2). A flag l is used to indicated the size of the input. janusROM> mm l 11200000 11200000 := 00000005 (or 5) 11200004 := 00000008 11200008 := 00000002 1120000C := <Enter>

As you might expect, variable data and program data are in the same format. Given an address, its impossible to tell if its just data or part of the program. Its up to the programmer 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, you can 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 in register D1. To check the values of registers, use the rd (register display) command. 13

janusROM>rd Table 0.8: Register display reference Register Display


rd [-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 careful debugging. Poor debugging skills waste enormous amounts of time! Pay careful attention to the techniques 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 and the user can check the memory or registers for correctness. The user can resume execution. In our example, lets set breakpoints at address 0x10200006 and 0x10200016. Issue the following commands to add the breakpoints: janusROM>br 10200006 janusROM>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 of the breakpoint. At this point you may examine the contents of the registers or other memory locations using any of the janusROM commands. If you forget the name of a particular command, a quick help command comes in handy. To resume from a breakpoint, cont (continue) is used. Table 0.9: Breakpoint reference Breakpoint
break [-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 and displays all the registers after each instruction. Effectively, the result of an rd command is displayed after each instruction. Trace can be used to identify problems in a program. To trace from the beginning 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 trace mode. Whenever the <Enter> key is hit, the monitor will execute the instruction at the current program location. To resume regular execution you can toggle the trace mode back to off using the continue command, cont, with a t flag to toggle the trace mode: janusROM>cont t Table 0.10: Continue reference Continue
cont [-h] [-t] Options: -h -t show this message. Toggle trace mode.

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

Breakpoint with trace


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

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

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

16

Lab 1: Hand Assembling


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

Objectives

Prelab reading
Introduction
Assembly language is more readable and suitable for programming than machine language. In this lab, you will encounter a piece of assembly code and learn how the assembly instructions are translated, or assembled, into machine code.

Project description
In this lab, you will write the assembly code and hand assemble it into machine code. A partial program, which has the input and output subroutines, is assembled and ready to download to the board. The partial program is listed below. Your assignment is to complete a small program that reverses a character string. The code is provided to you in an assembly listing format. The first column is the address of the instruction, 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 make reading 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 8 assembly instructions that are provided for you to assemble as well as a segment of code that you will need to write and assemble. The addresses or each instruction are also to be calculated. You are to hand assemble and write the required code before coming to your first lab session. Be sure to attend 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/ Operands Directives

CPU 5307 include q:\eng\ece\Coldfire\coldfire.asm * E&CE 222 Lab 1 sample program. * * Copyright 1998, University of Waterloo 10200000 10210000 10220000 10230000 0000000d 0000000a 10210000 10210000 5468 726f 2077 7072 6f75 7374 2069 6520 7273 6465 1021003c 456e 7374 2074 6f63 2066 6973 6772 696c 696e 7420 7269 6e20 7265 6520 722e 7465 7269 6f20 6573 6f6c 2070 PROMPT 616d 6c20 7420 6120 6e67 7468 7665 6f72 0d0a 7220 6e67 7072 732c 6c6f PROGRAM OPERAND BUFFER STACK CR LF equ equ equ equ equ equ org dc.b $10200000 $10210000 $10220000 $10230000 $0D $0A OPERAND "This program will print out a string in the reverseorder.",CR,LF

dc.b

"Enter string to process, followed by :",0

18

7765 6420 6279 203c 456e 7465 723e 3a00 10200000 org * Initilization 10200000 4ff9 1023 0000 10200006 4ef9 1020 0094 lea jmp STACK,a7 start ;initialize stack PROGRAM

****************************************************** * * 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_string 1020000c 1020000e 10200010 10200012 10200014 1020001a 1020001c 1020001e 10200024 10200028 1020002e 10200032 10200034 10200036 10200038 1020003a 1020003c 2f01 2f02 2f09 7400 4eb9 4a81 67f6 b2bc 6700 b2bc 6700 12c1 5282 60dc 4a82 67d8 12bc D1,-(A7) D2,-(A7) A1,-(A7) #0, D2 1020 0082 in_string_loop jsr in_char tst.l D1 beq in_string_loop 0000 000d cmp.l #CR,D1 0012 beq.w exit_in_string 0000 000a cmp.l #LF,D1 0008 beq.w exit_in_string move.b D1,(A1)+ add.l #1, D2 bra in_string_loop exit_in_string tst.l D2 beq in_string_loop 0000 move.b #0,(A1) move.l move.l move.l move.l

; in D2 keep a count of how many characters we have

; ignore null characters if we get them

; Note LF gets caught on CF-server when prog run!

; loop back if we have 0 characters

19

10200040 10200042 10200044 10200046

225f 241f 221f 4e75

move.l move.l move.l rts

(A7)+,A1 (A7)+,D2 (A7)+,D1

****************************************************** * print CR, LF ****************************************************** out_crlf 10200048 2f01 1020004a 1020004e 10200054 10200058 323c 4eb9 323c 4eb9 000d 1020 0078 000a 1020 0078 move.l move jsr move jsr move.l rts D1,-(A7) #CR,D1 out_char #LF,D1 out_char (A7)+,D1

1020005e 221f 10200060 4e75

****************************************************** * Put out a string to the terminal * A1 points to start of string; 0 marks end of string ****************************************************** out_string 10200062 2f01 10200064 2f09 out_next_char 10200066 10200068 1020006e 10200070 10200072 10200074 10200076 1219 4eb9 1020 0078 4a01 66f4 225f 221f 4e75 move.b jsr tst.b bne move.l move.l rts (A1)+,D1 out_char D1 out_next_char (A7)+,A1 (A7)+,D1 move.l move.l D1,-(A7) A1,-(A7)

****************************************************** * D1 is printed to terminal ****************************************************** out_char

20

10200078 1020007a 1020007c 1020007e 10200080

2f00 7013 4e4f 201f 4e75

move.l D0,-(A7) move.l #$0013,d0 TRAP #15 move.l (A7)+,D0 rts

;Select function

****************************************************** * D1 is returned from the terminal ****************************************************** 10200082 2f00 10200084 10200086 10200088 1020008e 7010 4e4f c2bc 0000 007f 61e8 in_char move.l move.l TRAP and.l bsr move.l rts D0,-(A7) #$0010,d0 #15 #$7F,d1 out_char (A7)+,D0

; Echo the character back to the user

10200090 201f 10200092 4e75

****************************************************** * * End of Subroutines * ****************************************************** 10200094 43f9 1021 0000 start 1020009a 6100 ffc6 1020009e 6100 ffa8 lea bsr.w bsr.w * Get input 102000a2 43f9 1022 0000 102000a8 6100 ff62 102000ac 6100 ff9a lea BUFFER,A1 bsr.w in_string bsr.w out_crlf * Scan for end of string. 102000b0 207c 1022 0000 move.l #BUFFER,A0 ;point to start of buffer ;point to start of buffer ;read string and add a trailing NULL ;go to next line PROMPT,a1 out_string out_crlf ;point to start of message ;print message ;go to next line

21

Enter the bloop1 calculated memory addresses and assembled opcodes here. Be sure to follow the format given above.

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 bsr.w * Exit move.l TRAP end #$0000,d0 #15 ;optional, marking end of assembly out_string out_crlf ;print the string ;and go to new line

22

23

Code Review
There are one-word, two-word, or three-word instructions. Some instructions take up to three words to represent them. Therefore, the instruction move.l $12000000 $13000000 is invalid because it would require five words to assemble into machine language (opcodes). It is important that you recognize why this is. If you do not, then this might be a good time to get acquainted with your TAs. Take a look at address 0x10210000 at the beginning of the program. The whole chunk of opcodes originating at this address is the hexadecimal representation of the prompt messages encoded in ASCII. In Lab 2, you will add prompt messages this way. Note that all strings are terminated with a 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? If not, 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 emulator software to produce a new line. The CR character returns the cursor to the beginning of the line while LF moves the cursor to the next line. Carriage return and line feed parlance comes from the days of the typewriter. There are 3 actions associated with TRAP #15 routines. Once the TRAP #15 instruction is executed it looks at the content of Register D0 (function number) to determine the action. The TRAP instruction is what is called a software interrupt. You will learn more about interrupts in Lab 4. For now, think of the TRAP #15 instruction as a way for your program to communicate with the janusROM 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 single character input from the serial port and stores it to the lower byte of D1. (See Section 1 The 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 ASCII format. (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 this method to mark the end of the program and exit gracefully. (See 1.1.4 the last two lines of the code)

Preparation
You should write and hand-assemble the required code before coming into the lab. In one lab session 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, this is impractical. JanusROM provides a means to download the program into the RAM via the serial port. 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 format encapsulates the opcodes with the addresses and checksums. This S-19, or object file, can be downloaded as explained in Lab 2. The second form of downloading, using monitor commands, will be used here. The first part of the program has been assembled for you and is ready for download. The OPCODES.TXT file on UWACE 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. The OPCODES.TXT file effectively uses the memory modify (mm) janusROM command to enter the program. If we send the OPCODES.TXT file with the terminal program, janusROM cannot tell it was not typed by hand. To download, make sure your system is sitting at the janusROM prompt. Then, in HyperTerminals menu bar, go to Transfer or Send Text File . Select to view all file types and then choose the OPCODES.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 Desktop are part of your roaming profile and are actually saved on the local machine. In the event of a network outage or an improper log-out, these files will be lost. The N:, on the other hand, is a mapped 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 also add your hand assembled and newly written opcodes to the previous file and transfer again as described above.

Run the program


Execute the program and check for correctness. If there are any errors, use breakpoints, traces and disasm 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 name checked off. Questions about the hand assembly methods and/or the functionality of the given program will be asked.

Post Lab
All execution is done in binary. The input and output of a program are typically displayed in, ASCII encoded, decimal format for readability. As a result, we need to have subroutines to convert data between 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 each string as an end of string delimiter. The in_string subroutine given in the code segment above appends a NULL character to the end. Similarly, the out_string routine requires a NULL character to delimit the end of a string. Therefore, to convert the ASCII character input string "3210" to binary form represented as 0xC8A in hex you have to subtract 0x30 from each digit and apply Horners 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 BUFFER containing the character string 3210 will be: Table 1.11: BUFFER contents byte-addressed
Address BUFFER BUFFER+1 BUFFER+2 BUFFER+3 BUFFER+4 Contents

0x33 0x32 0x31 0x30 0x00

Table 1.12: BUFFER contents word-addressed


Address BUFFER BUFFER+2 BUFFER+4 Contents

0x3332 0x3130 0x0000

Table 1.13: BUFFER contents longword-addressed


Address BUFFER BUFFER+4 Contents

0x33323130 0x00000000

26

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 several times and collect the remainders. Assume an unsigned input of 16 bits in register D1. The range will be from 0 (0x0000) to 65,535 (0xFFFF). For example, converting 1345 into an ASCII number would be accomplished by successively dividing by 10 and collecting the remainders as: 1345/10 = 134:5 134/10 = 13:4 13/10 = 1:3 1/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 reversed version of the desired result. All that remains is to reverse the character string, append the NULL character and were done. Your assignment for the post lab is to write your own BIN2DEC (register word to ASCII) and DEC2BIN (ASCII to register word) subroutines in assembly language. You are not required to hand-assemble them. The DEC2BIN and BIN2DEC routines you prepare as part of this post lab will 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 double checking your calculations, is to use the disasm (or di)command in janusROM. This is one of the most 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 board will halt the program and display the address before the halt. This enables you to debug the problem by identifying the portion of your code where execution is stuck. However, this does not always work 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 in this case to separate the data from the executable program code. One method of constructing an assembly program is to simply have one origin (the start of memory, for example) and let the assembler place everything in memory. A drawback of this technique is that you do not know where anything is in memory until youve assembled the program and looked at the assembly listing to locate 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 in logical partitions based on function. The most basic way of dividing memory is to separate the code, data and stack. If you notice that execution of your program has entered the code segment or that the stack has grown to into your code or data you will instantly recognize the fault by checking the 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 common error is for the stack to overflow into program or data segments. Ensure that you load an appropriate address into the stack pointer A7 as is done on the second page of the code listing provided 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 pseudo ops or special assembly commands that are available to you. Pseudo ops are not documented in the programmers manual because they tend to be specific to each assembler. The assembler documentation is available on the course website. Consult the assembler documentation for detailed descriptions of the most common pseudo ops: ORG (ORiGin), DS (Define Storage), DC (Define Constant), EQU (EQUate) and ALIGN.

28

Potrebbero piacerti anche