; Title: CISS 360 Programming Assignment
;------------------------------------------------------------
; Description: This program reveals the code necessary to
; convert a numeric string entered at the key-
; board to an integer value (essentially it's
; an implementation of the "get_i" function).
;------------------------------------------------------------
; Programmer: Ron Hart
;------------------------------------------------------------
; Date/Time: Sunday, February 13, 2011 2:48 PM
;------------------------------------------------------------
.586
.MODEL FLAT
INCLUDE console_io.h ; header file for console input/output
.STACK 4096 ; reserve 4096-byte (1024 DWORD) stack
.DATA ;****************************************************
;* Data Section *
;****************************************************
prompt BYTE 'Enter a positive or negative integer: ',0
result BYTE 'You entered: ',0
newline DWORD 10
.CODE ;****************************************************
;* Code Section *
;****************************************************
_MainProc PROC
put_str prompt ; prompt user for an integer value
call my_get_i ; invoke conversion routine
put_str result ; display label for result
put_i eax ; display integer value
put_ch newline ; display newline
put_ch newline ; and another
main_done:
done ; display "end-of_program" message
xor eax, eax ; exit with return code 0
ret
_MainProc ENDP
;------------------------------------------------------------
my_get_i PROC
push ebp ; save base pointer
mov ebp, esp ; establish stack frame
push ebx ; save registers used by this proc
push edx
xor edx, edx ; accumulate integer value in EDX
mov ebx, 1 ; EBX indicates sign (1 means positive, -1 means negative)
next_char:
get_ch ; input character to EAX
cmp al, ' ' ; al = space?
je next_char ; yes, bypass and get next character
cmp al, 9 ; no, al = tab?
je next_char ; yes, bypass and get next character
cmp al, '-' ; no, al = minus sign?
je negative ; yes, go set "negative" flag
cmp al, '+' ; no, al = plus sign?
je next_byte ; yes, go get next byte of input
cmp al, 10 ; no al = newline?
je fini ; end of input string, finish up
jmp check_digit ; go check byte for a digit
negative:
neg ebx ; make EBX negative (indicates negative value entered)
next_byte:
get_ch ; input next character to EAX
check_digit:
cmp al, 48 ; AL < ascii zero?
jl fini ; yes, go finish up
cmp al, 57 ; no, AL > ascii nine?
jg fini ; yes, go finish up
sub al, 48 ; no, convert ascii digit to integer
imul edx, 10 ; increase current value by power of ten
add edx, eax ; add in current integer byte
jmp next_byte ; go process next input character
fini:
mov eax, edx ; returned integer value in EAX
imul ebx ; set sign of result
pop edx ; restore registers used by this proc
pop ebx
pop ebp ; restore base pointer
ret
my_get_i ENDP
;----------------------------------------------------------------------
END ; end of source code
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.