Lab 3

advertisement
Laboratory 3: Converting from ASCII(American Standard Communications
Information Interchange) to Binary
Type in the following programme using EMU8086 and include your
Name, Class, Group, Year and Date. Save this programme as file:
lab_3.asm.
;
;
;
;
;
TITLE
NAME
DATE
CLASS
GROUP
Purpose of Lab
Introduction to a procedure for reading the keyboard and converting the
ASCII value to its binary equivalent.
REQUIREMENTS:
You are required to type in the relevant parts of the procedure below
using the EMU8086 Emulator.
SPECIFICATION:
1. Open a new .EXE template in the EMU8086 emulator.
2. Copy the Lab 3 programme below into the relevant position of the
EMU8086 editor.
3. Assemble and Emulate the programme using the Emulate option
from the menubar.
4. Run the programme using the Single Step option from the
menubar.
5. Examine the contents of the AX register.
6. Finally, write a report and explanation of the 8086 assembly
language procedure structure in an .EXE programme.
DESIGN:
You are expected to try and understand the flow of the programme.
IMPLEMENTATION:
Conversions from ASCII to binary usually start with keyboard data entry.
If a single key is typed the conversion is accomplished by subtracting a
30H from the number. If more than one key is typed, conversion from
ASCII to binary still requires that 30H be subtracted, but there is one
additional step. After subtracting 30H, the prior result is first multiplied
by 10, the number is added to the result. The algorithm used to convert
ASCII to binary is:
1.
2.
3.
4.
Begin with a binary result of 0.
Subtract 30H from the character typed on the keyboard to
convert it to BCD.
Multiply the binary result by 10 and add the new BCD digit.
Repeat steps 2 and 3 until the character typed is not an ASCII
coded number of 30H-39H.
Example 1 illustrates a procedure that implements the ASCII-to-binary
conversion algorithm. Here the binary number returns in the AX register
as a 16-bit result. If a larger result is required, the procedure must be
reworked for 32-bit arithmetic. Each time this procedure is called, it reads
a number from the keyboard until any key other than 0 through 9 is typed.
It then returns with the binary equivalent in the AX register.
Lab 3 Programme: Example 1
;*************************************************************
READN
PROC NEAR
PUSH AX
PUSH CX
MOV CX,10
XOR BX,BX
;save BX and CX
;load 10
;clear result
READN1:
MOV AH,01H ;read key
INT 21H
CMP AL,'0'
JB READN2
CMP AL,'9'
JA READN2
;test against 0
;if below 0
;test against 9
;if above 9
SUB AL,'0'
;convert to BCD
PUSH AX
MOV AX,BX
MUL CX
MOV BX,AX
POP AX
XOR AH,AH
ADD BX,AX
JMP READN1
;Multiply by 10
;save product
;add BCD to product
;repeat
READN2:
READN
MOV AX,BX ;move binary to AX
POP CX
;restore BX and CX
POP BX
RET
ENDP
TITLE 8086 Code Template (for EXE file)
;
;
;
;
AUTHOR
emu8086
DATE
?
VERSION
1.00
FILE
?.ASM
; 8086 Code Template
; Directive to make EXE output:
#MAKE_EXE#
DSEG SEGMENT 'DATA'
; TODO: add your data here!!!!
DSEG ENDS
SSEG SEGMENT STACK 'STACK'
DW 100h DUP(?)
SSEG ENDS
CSEG SEGMENT 'CODE'
;*******************************************
START PROC FAR
; Store return address to OS:
PUSH DS
MOV AX, 0
PUSH AX
; set segment registers:
MOV AX, DSEG
MOV DS, AX
MOV ES, AX
CALL PROC1_NAME
CALL PROC2_NAME
; return to operating system:
RET
START ENDP
;+++++++++++++++++++++++++++++++++++++++++++++++++++
PROC1_NAME
PROC NEAR
; TODO: add your PROCEDURE 1 here!!!!;
RET
PROC1_NAME ENDP
;+++++++++++++++++++++++++++++++++++++++++++++++++++
PROC2_NAME
PROC NEAR
; TODO: add your PROCEDURE 2 here!!!!;
RET
PROC2_NAME ENDP
;*****************************************************
CSEG ENDS
END START ; set entry point.
Download