Spring 2002 CSCI 255 Lab 12

This lab is scheduled for 8 & 9 May.

Goals and Methods

This week we'll look at:

  1. Finishing out subroutine program
  2. Writing IA-32 assembly code

Introduction to IA-32 programming

We have three short tasks for you this week. Begin by executing the following commands to create a directory csci/255/lab12 and copy seven files into it.

Notice that one of these files is a special Unix control file called a Makefile for compiling (and assembling) programs. To compile files within your directory type the following command:

This command will run the C compiler, gcc, twice to compile the C programs main12a.c and main12b.c. It will run an assembler, nasm, four times. It will also run the linker twice, also using gcc, to produce the executable programs lab12a and lab12b.

You've built the program lab12a. Now run it by typing:

Take a look at the main routine for lab12a. It's written in C.


#include <stdio.h>

void readdec(int *, int *) ;
int prnthex(int) ;

int main(int argc, char *arvg[], char *envp[]) {
  int R0, R1, R2, R3 ;

  fputs("Enter a postive decimal number> ", stdout) ;

  /* Read in the integer as a decimal */
  R2 = 0 ;
  while (1) {
    readdec(&R0, &R1) ;
    if (R0 << 0)
      break ;
    R2 = 10*R2 + R1 ;
  }

  fputs("\nIn hexadecimal your number is ", stdout) ;
  for (R3=4; R3>0; --R3) {
    R0 = R2 >> 12 ;	/* Hard to do in the LC-2 */
    R0 = prnthex(R0) ;
    putc(R0, stdout) ;
    R2 = R2 << 4 ;
  }
  putc('\n', stdout) ;
}

The main routine reads in a sequence of digits and uses readdec to translate a single ASCII character into a binary numbers. Later it uses prnthex to print a number in hexadecimal.

Optimizing the code

Here's a C version of the readdec routine.


  void readdec(int *R0, int *R1) {
   int T ;
    T = getc(stdin) ;		/* getc will echo automatically */
    if (('0' <= T) && T <= '9') {
      *R1 = T - '0' ;
      *R0 = 0 ;
    } else
      *R0 = -1 ;
    return ;
  }

The assembly language version of readdec works correctly, but you're going to improve this assembly routine, which incidently was produced by the gcc compiler, by removing at least one line of code. This isn't so hard. Just keep in mind that, unless you're getting ready to make a conditional branch, there's no point in a mov if the source and destination are the same.

Bring order to chaos

Now we'll look at the prnthex routine. Here's the equivalent C code.


  int prnthex(int X) {
    X = X & 0xF ;
    if (X < 10)
      return X + '0' ;
    else
      return X + ('A'-10) ;

Load prnthex.asm into your pico. You'll notice that right in the middle of the program that six lines of assembly code have been rearranged by accident. You're job is to get them back in the right order. The C code at the top of the file should be a bit help. There are only 720 possibilities, so you should finish before next week.

Recompiling the program

You can use the program make to recreate lab12a and then test your work. Take a minute to see how the Makefile guides this compilation process.

Fixing another bug

Finally, we turn our attention to the lab12b program. It has a buggy multadd routine. pico multadd.asm and examine the code. This program is supposed to compute (W*X)+(Y*Z) but instead it is computing (W+X)*(Y+Z). Fix the problem, recreate lab12b, and test your work.

Going home

Do not delete your files. Your lab instructor will examine them later.