Автор: ne_viens
Дата сообщения: 10.05.2014 16:35
Masm[more]
Код: ; ml getlims.asm /link /SUBSYSTEM:CONSOLE
.386
.model flat, stdcall
option casemap :none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
.const
g_prompt1 db "Enter the input file name:",10
fmat db "Array min value = %d, max value = %d", 10, "Enter the output file name:", 10, 0
g_err db "Error: Can not open an input file.",10
g_err1 db "Error: Input file is empty.",10
g_err2 db "Error: Can not allocate memory.",10
g_err3 db "Error: File read error.",10
g_err4 db "Error: Can not create an output file.",10
.code
;###########################################################
puts proc, msg, len
LOCAL tmp
push 0
lea eax, tmp
push eax
mov ecx, len
push ecx
push msg
invoke GetStdHandle, STD_OUTPUT_HANDLE
push eax
call WriteFile
mov eax, -1
ret
puts endp
;###########################################################
main proc USES ebx esi edi
LOCAL tmp
LOCAL buf[MAX_PATH]:BYTE
;---------------------------------input
invoke puts, OFFSET g_prompt1, SIZEOF g_prompt1
invoke GetStdHandle, STD_INPUT_HANDLE
lea esi, buf
lea edx, tmp
invoke ReadFile, eax, esi, SIZEOF buf, edx, 0
mov ecx, tmp
mov byte ptr[esi+ecx-2], 0 ;cut off /r/n
invoke CreateFile, esi, GENERIC_READ, 0, 0, OPEN_EXISTING, 0, 0
mov edi, eax
inc eax
jz err
invoke GetFileSize, edi, 0
mov ebx, eax
or eax, eax
jz err1
invoke VirtualAlloc, 0, ebx, MEM_COMMIT, PAGE_READWRITE
mov esi, eax
or eax, eax
jz err2
invoke ReadFile, edi, esi, ebx, ADDR tmp, 0
invoke CloseHandle, edi
cmp tmp, ebx
jne err3
xor ecx, ecx
mov eax, [esi+ecx]
mov edi, eax
;---------------------------------comparing loop
_loop:
add ecx, 4
cmp ecx, ebx
jge getOut
mov edx, [esi+ecx]
cmp eax, edx
jge @F
mov eax, edx
@@:
cmp edi, edx
jle @F
mov edi, edx
@@:
jmp _loop
;---------------------------------output
getOut:
mov ebx, eax
invoke VirtualFree, esi, 0, MEM_RELEASE
invoke wsprintf, ADDR buf, OFFSET fmat, edi, ebx
invoke puts, ADDR buf, eax
invoke GetStdHandle, STD_INPUT_HANDLE
lea esi, buf
lea edx, tmp
invoke ReadFile, eax, esi, SIZEOF buf, edx, 0
mov ecx, tmp
mov byte ptr[esi+ecx-2], 0 ;cut off /r/n
invoke CreateFile, esi, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0
mov edi, eax
inc eax
jz err4
mov[esi], edi
mov[esi+4], ebx
invoke WriteFile, edi, esi, 8, ADDR tmp, 0
invoke CloseHandle, edi
xor eax, eax
_ret:
ret
;---------------------------------all errors are here
err:
invoke puts, OFFSET g_err, SIZEOF g_err
jmp _ret
err1:
invoke CloseHandle, edi
invoke puts, OFFSET g_err1, SIZEOF g_err1
jmp _ret
err2:
invoke CloseHandle, edi
invoke puts, OFFSET g_err2, SIZEOF g_err2
jmp _ret
err3:
invoke VirtualFree, esi, 0, MEM_RELEASE
invoke puts, OFFSET g_err3, SIZEOF g_err3
jmp _ret
err4:
invoke puts, OFFSET g_err4, SIZEOF g_err4
jmp _ret
main endp
;###########################################################
start:
invoke main
invoke ExitProcess, eax
end start