IUP Computer Science
COSC 300

Exercise #3

Below is a program that is supposed to read an unsigned integer from the keyboard and then display it on the screen.  The integer is read with ReadInt; but the display is created by taking the integer and converting it to characters and displaying it one character at a time; this is basically what WriteDec does to display numbers.  The problem is that the program does not quite work.  I'll give you a hint about its problems:  it is missing one instruction and there is one other instruction that is out of place.  When the program is executed and the number 1234567 is entered, it displays as follows (rather than displaying 1234567 as it should).

Enter the number: 1234567

 ☺☻♥♦♣♠
Press any key to continue

Use the debugger to determine what is wrong with the program and fix it.  You can get a copy of the program from the Z: drive at Z:\jlwolfe\cosc300\convert.asm

        title        Convert an integer for display
        include      irvine32.inc
        .data
prompt  byte        "Enter the number: ",0
value   dword       ?                 ; value to convert
base    dword       10                ; base to use
temp    dword       ?                 ; temporary for partial result
digits  byte        20 dup(?)         ; place to hold digits
        .code
main proc
        mov        edx, offset prompt
        call       writestring           ; prompt and read in number
        call       readint
        mov        value, eax   
        mov        esi,offset digits     ; put offset of digits in ESI
        mov        ecx,0                 ; initialize counter
        mov        eax, value            ; get original value
top:
        cdq
        div        base                ; divide by the base
        mov        [esi],dl            ; store remainder in digit string
                                       ; digits are produced in reverse
        inc        esi                 ; move to next position
        inc        ecx                 ; count digit
        cmp        eax, 0              ; check for end
        jg         top
round:
        mov        al,[esi]
        call       writechar           ; display the digit
        dec        esi                 ; back up one digit
        loop       round               ; count down with ECX until
                                       ; all digits are displayed
        call       crlf                ; display cr, lf to end
        exit
main endp
end    main

The technique used in the program is to repeatedly divide by 10 (base) each time getting a quotient and a remainder.  The remainders are in the range 0 to 9 and need to be converted to an ASCII digit string.  The characters of the digit string are displayed in reverse order.  Writechar is used to display each digit character.  The esi register is used to access the characters of the digit string indirectly.

Once you have the program working, try adding the following capabilities to it.
  1. After prompting for the number, prompt for the base and display the number in the specified base.  The base may be any number from 2 through 10.
  2. Allow the number entered to be negative and properly display it with a leading minus sign.