CSCI 110, Spring 2011

    Home | | Course Schedule | | Assignments | | Lecture Notes

    Homework 8 solution

    Problem 1.
    Consider the following assembly language program:

           IN    AX
           COPY  M1, AX
           SUB   AX, M1
           CMP   AX, M1
           JB    LAB1
           OUT   AX
           JMP   LAB2
      LAB1 COPY  AX, M1
           DIV   AX, M1
           OUT   AX
      LAB2 END
      

    a) What is the output of the above program if the input is 4?

      Answer: 1

    b) What is the output of the above program if the input is 0?

      Answer: 0

    c) Briefly describe how the program works.

      The program reads in a number and stores it in memory. It then puts a zero in the accumulator by subtracting the number from itself. It compares zero to the number entered. If zero is less than the number entered, then the program copies the number back into the accumulator, and divides it by itself, creating the number 1. It then prints out 1. If zero is greater than or equal to the number entered, the program prints out 0 (the number that is in the accumulator) and then jumps to the end.

    Problem 2.
    Write an assembly language program that reads two integers and prints their sum.

      	IN AX
      	COPY X, AX
      	IN AX
      	ADD AX, X
      	OUT AX
      	END
      	

    Problem 3.
    P88 Instructions to enter 2 numbers and print out the larger of the two:

    There are many possible solutions. Here is one:

            IN      AX           {Enter 1st number}
            COPY    X, AX        {Copy 1st number into X}
            IN      AX           {Enter 2nd number}
            COPY    Y, AX        {Copy 2nd number into Y (optional)}
            CMP     AX, X        {if AX < X set CF = B, else CF = NB}
            JNB     PRINT        {if AX >= X print it out}
            COPY    AX, X        {copy 1st number back into AX, since it's biggest}
    PRINT   OUT     AX           {Print out number that's in accumulator}
            END
    

    Alternative solution:

            IN      AX           {Enter 1st number}
            COPY    X, AX        {Copy 1st number into X}
            IN      AX           {Enter 2nd number}
            COPY    Y, AX        {Copy 2nd number into Y (optional)}
            CMP     AX, X        {if AX < X set CF = B, else CF = NB}
            JB      LABEL1       {if AX < X jump to LABEL1}	
            OUT     AX           {if AX >= X print it out}
            JMP     LABEL2       {Jump to the end}
    LABEL1  COPY    AX, X        {Copy from X to the accumulator}
            OUT     AX
    LABEL2  END