Homework 2 
+-*/ operations
Analysis/Design/Implementation


Overview

Assembly language programmers benefit greatly from program analysis and design methods such as those studied in C201 and C202. It consists of:
  1. Analysis of problem (determination of input and output, decomposition of problem into module structure chart).
  2. Design of algorithm (pseudo code and variables of algorithm for each module of structure chart).
  3. Implementation (translation to target language).
Recall that the target language influences the algorithm's design only slightly. It is STRONGLY urged that you practice this design method as test questions will often provide pseudo code algorithms for implementation in Assembler language.

Assignment

Design and implement a high-level algorithm to convert a time from seconds to hours, minutes, and seconds. After verification of the high-level algorithm operation, translate your algorithm into assembler. Your input should be the time in seconds entered via the keyboard. The input value will be in the range 0 to 65535. The output should be the equivalent time in the form HH:MM:SS. For example, an input of 7272 seconds would yield output of 2:1:12.  Test using the values of:

                    0, 59, 60, 3599, 3600, 7272, 32000, 32072, 65535

Turn in

  1. Cover sheet with your name, date, and Homework 3.
  2. High-level language solution (C++, Basic, Pascal, etc.). Give program listing of solution and test output using values given above.
  3. Assembly language solution. Give program listing of solution and test output using values given above. For documentation and as helpful reminder of algorithm when debugging assembly code, include pseudo code comments to the right of assembly instructions.

Hints

  1. Use the following example program as a guide for program structure.
  2. The input should be an unsigned 16-bit number. Use GetDec$ to read an unsigned integer from the keyboard into the Ax register.
  3. The author's string output function, PutStrng, requires the Es register to point to the location of the string to be output. Suggest using code similar to that in the example program to initialize the Ds and Es registers.
  4.         Mov     Ax, Seg Data
            Mov     Ds, Ax
            Mov     Es, Ax
  5. You will need to divide the input number to determine the number of hours, minutes, seconds. Remember that division produces a quotient and a remainder.
  6. Remember the rules for division, a 16-bit divisor automatically uses Dx:Ax as the dividend, producing Ax as the quotient and Dx as the remainder. Make certain that Dx:Ax have been properly initialized before the division (i.e. Mov Dx, 0 for unsigned; CWD for signed division).
    1.         Ax   
      16-bit/ Dx:Ax
              Dx

Examples

The text gives an Assembly language solution for converting Fahrenheit to Centigrade on page 145. The corresponding Pascal and C++ solutions are given below. Included is a different Assembly solution produced by directly translating the C++ to Assembly language. Note the Assembly comments consist of the high-level algorithm given in C++.
 
Pascal Fahrenheit to Centigrade Example
Var 
 Prompt : String[42] = 'ENTER TEMPERATURE IN DEGREES FAHRENHEIT';
 Annotation : String[42] = '  TEMPERATURE IN DEGREES CENTIGRADE';
 Ax     : Integer;
Begin
     Write( Prompt );
     Readln( Ax );                     { Read Fahrenheit }
     Ax := Round( (Ax - 32) * 5/9 );   { Convert Fahr. to Cent. }
     Write( Annotation ); 
     Writeln( Ax );                    { Write Centigrade }
End.
C++ Fahrenheit to Centigrade Example
#include <iostream.h>
void main() {
        char Prompt[42] = "ENTER TEMPERATURE IN DEGREES FAHRENHEIT";
        char Annotation[42] = "  TEMPERATURE IN DEGREES CENTIGRADE";
        int Ax;

        cout <<  Prompt ;
        cin >> Ax ;                             // Read Fahrenheit
        Ax = (int) ( (Ax - 32) * 5/9 ); // Convert Fahr. to Centigrade
        cout << Annotation ;
        cout << Ax ;                          // Write Centigrade
}

Assembler Fahrenheit to Centigrade Example
        Extrn   Getdec:Far              ; #include <iostream.h>
        Extrn   Putdec:Far
        Extrn   PutStrng:Far

Data    Segment
        Prompt          db      "ENTER TEMPERATURE IN DEGREES FAHRENHEIT ";
        Annotation      db      "  TEMPERATURE IN DEGREES CENTIGRADE     ";
Data    Ends

Code    Segment
        Assume  Cs:Code, Ds:Data, Ss: SSeg
main    Proc    Far                     ; void main()  {
        Mov     Ax, Seg Data
        Mov     Ds, Ax
        Mov     Es, Ax

        Mov     Cx, 40
        Lea     Di, Prompt
        Call    Putstrng                ;       cout <<  Prompt ;

        Call    Getdec                  ;       cin >> Ax ; 

        Sub     Ax, 32                  ;       Ax = (int)((Ax - 32)* 5/9 );
        Mov     Bx, 5
        Imul    Bx
        Mov     Bx, 9
        Idiv    Bx                      ;       Dx=Remainder     Ax=Quotient
        Xchg    Ax, Dx

        Mov     Bl, 5                   ;       Round up
        Idiv    Bl
        Cbw
        Add     Ax, Dx

        Mov     Cx, 40                  ;       cout << Annotation ;
        Lea     Di, Annotation
        Call    Putstrng

        Mov     Bh, 0
        Call    Putdec                  ;       cout << Ax ;                    

        Mov     Ah, 4ch
        Int     21h
  main  Endp                            ;       }
Code    Ends

SSeg    Segment        STACK       ;;Stack segment of 254 bytes
         DB             254 dup(?)
SSeg    Ends

        End     main


Input and Output

C++ uses cin for input and cout for output. In the Assembler example above, notice the lines beginning with EXTRN. These indicate names of routines (Putstrng for outputting a string, Getdec for inputting a signed number, etc.) separately written , assembled and object code stored EXTERNALLY as a library of routines. Comments in the program below indicate each routines purpose but the text contains a complete discussion on the use of each of these in Appendix D. The input and output routines are stored in library file name of io.lib and is available on the text diskette or by clicking on:
io.lib
The object code of a program using these input/output routines must be combined with the io.lib object code by the linker. The commands to assemble and link the following program source named Hw3.asm are:
ml /Fl Hw3.asm /link io.lib  

Execution

The program can be executed one step at a time using the debugger or all at once. For either of the two approaches, enter at the MS-DOS prompt:

Printing

  1. ml /Fl Hw3.asm /link io.lib     The /Fl generates a listing on Hw3.lst.
  2. Using WordPad, open the listing file Hw3.lst, enter File, Page Setup and set to print in landscape mode. Print.

Document last modified: