6502 Assembly Language String Lab

From CDOT Wiki
Jump to: navigation, search
Lab icon.png
Purpose of this Lab
In this lab, you will write code with string handling and character conversions in 6502 assembly language, in preparation for learning more complex x86_64 and AArch64 assembly language.



  • To convert a digit into a character, add the value of the ASCII character "0" ($30, 41). For example, $05 + $30 = $35, which is the ASCII code for the digit "5".
  • Decimal (BCD) mode may be useful for some of these problems.
    • To get the upper digit from a BCD byte, perform LSR four times to shift the upper four bits to the lower four bit locations and zero the upper four bits.
    • To get the lower difit from a BCD byte, AND the byte with $0f.
    • Remember to turn decimal mode on for operations on BCD bytes, and turn it off for other operations so that it does not affect binary math (use the SED and CLD instructions).
    • Decimal mode only affects add and subtract operations (ADC and SBC).
  • To indicate the cursor position, you may want to use reverse video, which is available in the emulator by ORing a printable ASCII code with $80 to turn on the high bit.
  • It is recommended that you run the emulator at its highest speed (move the Speed slider to the right).

Lab 4


1. Organize a group of 4-6 students around one of the monitor/whiteboard groups in the classroom. Arrange the furniture so that everyone has a comfortable view of the display.

2. Gather these supplies:

  • HDMI cable
  • Whiteboard markers

3. Select one person to be the "Driver", who will type/operate the computer for the group. That person should connect a device (laptop, table) to the HDMI display and open the 6502 Emulator at [1] as well as this Lab page. It's a good idea to ensure that the Speed slider on the emulator is at its lowest setting (left) and that the Text Screen is turned off (unchecked).

Sharing Results
Decide how group results will be shared between the members of the group. (Suggestion: consider using a git repository).
Save Your Work
The emulator does not save your work. Remember to periodically save it to a file (copy-and-paste the code or use the Save button to download it to your computer).

Pick Two Options

Pick and implement two of the following options for this lab. Requirements:

  • Use the emulator's ROM routines for at least one (and possibly both) problems.
  • In each case, make the software easy to use, with appropriate user prompts and feedback, and ensure that it looks good on the screen.

Option 1: Adding Calculator

  1. Create a subroutine which enables the user to enter two numbers of up to two digits. Indicate where the cursor is, and allow the user to use the digit keys (0-9), backspace, and enter keys. Return the user's input value in the accumulator (A) register.
  2. Using this subroutine, write a program which add the two numbers (each of which is in the range 0-99) and print the result.
  • Optional challenge: extend to more than 2 digits and add a running total capability with a clear key.

Option 2: Data Input Form

  1. Create a subroutine which enables the user to enter a string of up to 40 characters. Decide how the data will be returned from the subroutine. The user should be able to use the left/right cursor keys and backspace to edit the text, and the cursor location should be visible to the user.
  2. Write a program which uses this subroutine to collect the user's name, address, city, province, and postal code, and then display this information formatted as a shipping address (for example, as shown below).
Your order will be delivered to:

  J. Doe
  1750 Finch Ave E
  Toronto ON  M2J 2X5
  • Optional challenge: validate the province/territory and the postal code format.

Option 3: Hexdump

  1. Create a subroutine which enables the user to enter a 4-digit hexadecimal value. Allow the use of the backspace key for editing, and show the user the cursor location. Decide how the data will be returned by this subroutine.
  2. Using this subroutine, ask the user for a starting address and a length (in bytes), and then output a hexdump of the specified memory locations, formatted to show address and data information in some layout. If the output is longer than the screen, pause for user input (e.g., "Press ENTER to continue" or something similar) every 20 lines.
  • Optional challenge: add a display of ASCII data on the right-hand side, similar to the output of the linux command od -A x -t xz -v filename

Option 4: Screen Colour Selector

  1. Create a subroutine which displays on the character display a list of colours available on the bitmapped display, one colour per line, with one of the colours names highlighted in reverse video (white on black). The user may use the up/down arrow keys to change the highlighted row. Return the user's selection as a number in the accumulator (A) register when they press Enter.
  2. Using this subroutine, get a colour from the user, then fill the bitmap display with this colour, and allow the user to select a different colour.
  • Optional challenge: update the colour on the bitmapped display as the user scrolls through the colour-selector list.


Post an entry on your blog describing your experiments in this lab. Include:

  1. An introduction, so that someone who happens across your blog will understand the context of what you're writing about.
  2. The results from the two Writing Code portions of the lab, including the code, a description of how the code works, and the results produced. Credit other sources appropriately (i.e., font/graphics data, code snippets) and ensure that you are in compliance with the licenses of any code snippets used.
  3. Your experiences with this lab -- your impressions of the Assembly Language, what you learned, and your reflections on the process.

Remember to follow the Blog Guidelines as you write.