Getting Started with 68000 Assembly Language

We'll be using the NeXTs in CMC307 to study a new assembly language (by "new" I mean to say "newer than the PDP-8"). These are real NeXTs with Motorola 680x0 processors, not the Intel 80486-based machines running the NeXTStep operating system in CMC306. To program the NeXTs in assembly language, login to (or telnet to) a NeXT (charon, io, venus, triton, etc.) and get a Unix window open. Everything described below will be happening in the Unix environment.

Writing and running programs

  1. The basic structure of a 68000 program written for the black NeXTs is shown in the example program capitalize.s. Note that the place where execution is to begin must be marked with the label _main, and that this label must be made global by means of the ".globl" assembler directive.

    When your program begins execution, the top item on the stack is a return address. Thus, your main program should end with an "rts" instruction. To avoid bus errors and segmentation faults when this rts gets executed, you will need to make sure the stack pointer is back where it started. So any subroutine that puts stuff on the stack should clean up after itself.

  2. Your source code should be stored in a file whose name ends with ".s".
  3. To turn your source into an executable program, use the C compiler cc. For example, type cc -o myProgram myProgram.s to generate an executable named myProgram from a source file named myProgram.s.
  4. Now type myProgram, and away you go.

Using gdb

If you have an executable program, you can use the Gnu DeBugger (gdb) to run it and investigate its behavior. To get started, type the command gdb myProgram.

Here is a brief list of the gdb commands you might wish to use.

  1. r Run the program from the beginning.
  2. c Continue the program after stopping at a breakpoint.
  3. s Take a single step. That is, execute one instruction of the currently running program. Neither c nor s works unless you have first started the program running with r, and then stopped either with Ctrl-C or with a breakpoint.
  4. ki Kill the currently running program. Gdb will ask you whether you want "kill the inferior process." Though this may sound like eugenics, go ahead and say "yes."
  5. b *address Set a breakpoint at the given address. You can specify an address by label or by numeric value. For example, b *label and b *0x3f6c set breakpoints at the label "label" and at address 3f6c (hexadecimal), respectively.
  6. clear *address Remove a breakpoint.
  7. i b Short for "info breakpoints," this lists the current breakpoints.
  8. i r Short for "info registers," this command displays the contents of the registers.
  9. h Displays the top of a hierarchy of help menus. Sometimes the on-line help is helpful.
  10. q Quit.
  11. x/f address eXamine memory contents at the specified address. Note that no asterisk is needed here. (Let's raise money to send the gdb coders to user-interface-design school.) The "f" stands for "format." A couple examples will give you the idea.
    1. x/10i 0x3f60 This will display 10 instructions starting at address 0x3f60. Note that if 0x3f60 is in the middle of an instruction, this will give you gibberish that looks like legitimate code.
    2. x/7c 0x3f60 This will display 7 bytes as ASCII characters starting at address 0x3f60.
    3. x/7b 0x3f60 This will display 7 bytes by hexadecimal value starting at address 0x3f60.
    Other formats include d (decimal), o (octal), and x (hexadecimal), all of which usually display 32-bit long words in the appropriate base. Be warned, however, that previous versions of gdb have been a little bit buggy, so you may still get some odd behavior.




Jeff Ondich, Department of Mathematics and Computer Science, Carleton College, Northfield, MN 55057
(507) 663-4364, jondich@carleton.edu