Solution to - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

Solution to

Description:

lea dx,input_message. mov ah,9 ;display input prompt. int 21h ... lea dx,output_message. mov ah,9 ;move to new line and get. int 21h ;ready to display result ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 18
Provided by: timma87
Category:
Tags: lea | solution

less

Transcript and Presenter's Notes

Title: Solution to


1
Solution to 10, Page 115
  • Assembly Language Programming
  • University of Akron
  • Dr. Tim Margush

2
The Problem
  • Prompt for single Hex digit
  • '0' through '9', 'A' through 'F', 'a' through 'f'
  • Illegal entries must be detected
  • Display entry in decimal
  • 0 through 15
  • Ask to repeat the program
  • 'y' or 'Y' will repeat, anything else exits

3
Approach
  • The major task is data translation
  • Given an ASCII code for a Hex digit, determine
    the numeric equivalent (unsigned byte)
  • Given an unsigned byte (0-15), display one or two
    character decimal representation
  • The input/process/output step must be embedded in
    a do until loop controlled by the yes/no response
    of the user

4
Prompted Input
  • Display prompt on screen
  • DOS function 9 is convenient to do this
  • The prompt should be terminated with a
  • Wait for single character input
  • DOS function 1 will do this, returning with the
    ASCII code of the input character in AL and
    leaves the cursor to the right of the entered
    character
  • Output CR/LF

5
Code Detail - Input
  • begin_process
  • prompt and accept single char input
  • lea dx,input_message
  • mov ah,9 display input prompt
  • int 21h
  • mov ah,1 DOS input function
  • int 21h get single char response
  • mov bl,al for safe keeping
  • lea dx,output_message
  • mov ah,9 move to new line and get
  • int 21h ready to display result

6
Hex to Unsigned Conversion
  • Given a character, we divide the task into two
    parts
  • If entry is '0'-'9'
  • subtract '0' to get unsigned byte
  • 'A'-'F' or 'a'-'f'
  • convert to uppercase by subtracting ('a'-'A')
  • subtract ('A'-10) to get unsigned byte
  • Invalid data is detected after conversion

7
Code Detail - Hex to Unsigned
  • convert ASCII hex digit (bl) to unsigned
  • cmp bl,'9' check if decimal digit
  • jle decimal_size
  • cmp bl,'F' uppercase or lowercase?
  • jle uppercase
  • sub bl,'a'-'A' lower to uppercase
  • uppercase handle 'A'-'F' case
  • sub bl,'A'-10 'A'-'F' to unsigned
  • jmp short conversion_complete
  • decimal_size handle '0'-'9' case
  • sub bl,'0' convert to unsigned
  • conversion_complete

8
Detecting Illegal Input
  • After applying the conversion algorithm, we
    should have a byte in the range 0-15
  • If illegal data was entered, the result will fall
    outside this range
  • If illegal, display an error message and jump
    back to the top of the program to allow the user
    another chance

9
Code Detail - Illegal Input
  • verify byte in BL is legal (0-15)
  • cmp bl,0 if lt0, illegal
  • jl illegal
  • cmp bl,15 here if gt0
  • jle legal if also lt15, OK
  • illegal
  • mov ah,9 display error message
  • lea dx,illegal_message
  • int 21h
  • jmp begin_process start over
  • legal

10
Unsigned to Decimal
  • Given an unsigned byte (0-15) we break the task
    into three steps
  • If 10 or more, print a '1' and subtract 10
  • Convert the byte (0-9) to ASCII by adding '0'
  • Display the ASCII character

11
Code Detail - Unsigned to Decimal
  • output byte in BL (0-15) as decimal
  • mov ah,2 DOS output function
  • cmp bl,10
  • jl ones_place jmp if single digit
  • mov dl,'1' output leading '1'
  • int 21h
  • sub bl,10 isolate ones place
  • ones_place
  • add bl,'0' convert to ASCII
  • mov dl,bl prepare to output
  • int 21h

12
Outer Control Loop
  • A label marks the top of the loop
  • At the end of the processing task
  • Prompt user to repeat the task
  • Accept single character input
  • If entry is 'y' or 'Y', jump to top of loop
  • Otherwise, exit

13
Code Detail - Outer Loop
  • begin_process
  • main processing task goes here
  • lea dx,continue_message
  • mov ah,9 display continue prompt
  • int 21h
  • mov ah,1
  • int 21h get response
  • cmp al,'y' y means do again
  • je begin_process
  • cmp al,'Y' Y means do again
  • je begin_process

14
Data Elements
  • The only data we need to declare are the strings
    for output
  • Place a at the end of each string so we can use
    DOS output function 9
  • Embed CR/LF's at strategic locations for line
    control

15
Code Detail - Data Segment
  • .data
  • input_message db 13,10
  • db 'Enter a legal hex digit '
  • output_message db 13,10
  • db 'in decimal this is '
  • illegal_message db 'Illegal entry!'
  • continue_message db 13,10
  • db 'Try again? (Y/N) '

16
Putting It All Together
  • Add standard entrance and exit code
  • DS must be set to the start of the actual data
    segment so we can access the strings
  • When program is finished, we must exit gracefully
    to DOS
  • Remember the model and stack directives
  • See Hexable.asm for source file

17
Code Detail - Standard Entrance/Exit
  • .code
  • hexable proc
  • mov ax,_at_data ax gets segment number
  • mov ds,ax transfer to segment reg
  • program code goes here
  • mov ax,4c00h DOS return, no error
  • int 21h
  • hexable endp
  • end hexable
Write a Comment
User Comments (0)
About PowerShow.com