📄 lib32.asm
字号:
;----------------------------------------------------------------------
;----------------------------------------------------------------------
; Decaf Language Standard Libaray Source Code
; For x86 family require 80386 or higher CPU
;
; Technical feature for this version:
; 32-bits module
; 32-bits register
; Win32 Console Support
;
;
; Author information
; Mao Yanhua,maoyanhua@tsinghua.org.cn
; Multimedia Lab, Tsinghua University
; 2003-10-29
;
; Current Version
; Version 1.02 beta
; Add buffer mechanism for Console input
;
;
; History Versions
;
; Version 1.02 beta by Mao Yanhua
; Multimedia Lab, Tsinghua University
; 2003-10-29
; Add buffer mechanism for Console input
;
; Version 1.01 beta by Mao Yanhua
; Multimedia Lab, Tsinghua University
; 2003-10-21
; Some miniority modification is made to
; better support the Windows console
;
; Version 1.00 alfa by Mao Yanhua
; Multimedia Lab, Tsinghua University
; 2003-7-16
; Win32 Console Support
;
; Version 0.90 alfa by Mao Yanhua
; Tsinghua CS84
; 2001-12-23
; DOS 16bit Console support
;
;----------------------------------------------------------------------
;----------------------------------------------------------------------
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include \masm32\include\masm32.inc
includelib \masm32\lib\masm32.lib
.data
_@IOBufSize dw 1024
_@FmtPrintInt db "%d",0
_@FmtPrintStr db "%s",0
_@IOBuf db 1024 dup(?),0
_@InBuf db 1024 dup(?),0
_@InBufLen dd 0
_@InBufOffset dd 0
_@chBuf db ?,0
_@strMsgBoxCaption db "Decaf program halt",0
_@strRTErrWrongIndex db "Decaf runtime error: Array subscript out of bounds",13,10,0
_@strRTErrWrongSize db "Decaf runtime error: Cannot create negative-sized array",13,10,0
.code
;
;----------------------------------------------------------------------
; kernal function _@main
; '_@main' call void function 'main', which server as program entry point
;----------------------------------------------------------------------
_@main proc
call main
call _Halt
_@main endp
;----------------------------------------------------------------------
; kernal function _@PrintCh
; print a character on the screen
; Param 1 lowest byte : the character to print
;----------------------------------------------------------------------
_@PrintCh proc
push ebp
mov ebp,esp
mov eax,[ebp+8]
mov _@chBuf,al
invoke StdOut,addr _@chBuf
pop ebp
ret 4
_@PrintCh endp
;----------------------------------------------------------------------
; Standard libray function _PrintInt
; print a signed integer character on the screen
; Param 1 : the integer to print
;----------------------------------------------------------------------
_PrintInt proc
push ebp
mov ebp,esp
mov eax,[ebp+8]
invoke wsprintf,addr _@IOBuf,addr _@FmtPrintInt,eax
invoke StdOut,addr _@IOBuf
pop ebp
ret 4
_PrintInt endp
;----------------------------------------------------------------------
; Standard libray function _PrintString
; print a ASCIIZ string on the screen
; Param 1 : the address of the string to print
;----------------------------------------------------------------------
_PrintString proc
push ebp
mov ebp,esp
mov eax,[ebp+8]
invoke StdOut,eax
pop ebp
ret 4
_PrintString endp
.data
_@StringTrue db "true",0
_@StringFalse db "false",0
;----------------------------------------------------------------------
; Standard libray function _PrintBool
; print a bool value on the string
; Param 1 : the bool value to print
;----------------------------------------------------------------------
.code
_PrintBool proc near
push ebp
mov ebp,esp
mov eax,[ebp+8]
cmp eax,0
jz _@LoadFalse
mov eax,offset _@StringTrue
jmp _@PrintBoolPrint
_@LoadFalse:
mov eax,offset _@StringFalse
_@PrintBoolPrint:
invoke StdOut,eax
pop ebp
ret 4
_PrintBool endp
;----------------------------------------------------------------------
; Standard libray function _PrintEqual
; Return true(1) when two ASIIZ string given is equal
; when not equal return false(0)
; Param 1 & 2: the address of the two string to compare
;----------------------------------------------------------------------
_StringEqual proc
push ebp
mov ebp,esp
mov esi,[ebp+8]
mov edi,[ebp+12]
dec esi
dec edi
_@CharCompare:
inc esi
inc edi
mov al,[esi]
cmp al,[edi]
jnz _@ReturnFromStringEqualFalse
cmp al,0
jnz _@CharCompare
mov eax, 1
jmp _@ReturnFromStringEqual
_@ReturnFromStringEqualFalse:
mov eax,0
_@ReturnFromStringEqual:
pop ebp
ret 8
_StringEqual endp
;----------------------------------------------------------------------
; kernal function _@atoi
; convert the string given to 32bits int
; return the convert result
; Prarm 1 :the address of the string to convert
;----------------------------------------------------------------------
_@atoi proc near
push ebp
mov ebp,esp
mov esi,[ebp+8]
sub eax,eax
sub ebx,ebx
dec esi
_@atoiClearForwordSpace:
inc esi
mov cl,[esi]
cmp cl,' '
je _@atoiClearForwordSpace
cmp cl,'-'
jne _@atoiCheckNum
mov bl,1 ;set bl to 1 for negetive number
inc esi
_@atoiCheckNum:
mov cl,[esi]
cmp cl,0
jz _@atoiReturnVar
cmp cl,' '
jz _@atoiClearFollowSpace
cmp cl,'0'
jge _@atoiGE0
mov eax,0
jmp _@atoiReturn
_@atoiGE0:
cmp cl,'9'
jle _@atoiLE9
mov eax,0
jmp _@atoiReturn
_@atoiLE9:
mov edx,10
mul edx
sub edx,edx
mov dl,cl
sub dl,'0'
add eax,edx
inc esi
jmp _@atoiCheckNum
_@atoiClearFollowSpace:
mov cl,[esi]
cmp cl,0
jz _@atoiReturnVar
cmp cl,' '
jnz _@atoiReturn0
inc esi
jmp _@atoiClearFollowSpace
_@atoiReturn0:
mov eax,0
jmp _@atoiReturn
_@atoiReturnVar:
cmp bx,0
je _@atoiReturn
neg eax
_@atoiReturn:
pop ebp
ret 4
_@atoi endp
;----------------------------------------------------------------------
; Standard libray function _Alloc
; Alloc a block of memory to user
; Return the address of the memory block
; Param 1 : the size of the memory block that the user want
;----------------------------------------------------------------------
_Alloc proc
push ebp
mov ebp,esp
mov eax,[ebp+8]
invoke Alloc,eax
pop ebp
ret 4
_Alloc endp
;----------------------------------------------------------------------
; Standard libray function _ReadLine
; Read a line of character for the console
; Return the address of the string
;----------------------------------------------------------------------
_ReadLine proc
push ebp
mov ebp,esp
mov eax,_@InBufLen
mov ebx,_@InBufOffset
cmp eax,ebx
jnz _@ReadLineNoRead
mov _@InBuf,0
invoke StdIn,addr _@InBuf,1024
mov _@InBufOffset,0
mov _@InBufLen,eax
add eax,offset _@InBuf
mov byte ptr [eax],0
_@ReadLineNoRead:
mov esi, offset _@InBuf
mov edx, 0D0Ah
add esi, _@InBufOffset
push esi
sub ecx,ecx
sub eax,eax
_@ReadLineStartCompare:
mov al,[esi]
inc ecx
cmp al,dh
jz _@ReadLineFindCR
inc esi
cmp al,ah
jnz _@ReadLineStartCompare
dec ecx
mov ebx,_@InBufOffset
cmp ebx,0
jz _@ReadLineFindCR
cmp ecx,0
jz _@ReadLineFindCR
inc ecx
pop esi
mov esi,offset _@InBuf
mov edi,esi
add edi,_@InBufOffset
_@ReadLineLoop:
movsb
loop _@ReadLineLoop
mov eax,_@InBufOffset
sub _@InBufLen,eax
mov eax,offset _@InBuf
add eax,_@InBufLen
invoke StdIn,eax,_@InBufOffset
mov _@InBufOffset,0
add _@InBufLen,eax
mov eax,_@InBufLen
add eax,offset _@InBuf
mov byte ptr [eax],0
jmp _@ReadLineNoRead
_@ReadLineFindCR:
mov [esi],ah
add _@InBufOffset,ecx
mov al,[esi+1]
cmp al,dl
jnz _@ReadLineNot0A
mov [esi+1],ah
inc _@InBufOffset
_@ReadLineNot0A:
push ecx
call _Alloc
pop edi
invoke lstrcpy,eax,edi
pop ebp
ret 0
_ReadLine endp
;----------------------------------------------------------------------
; Standard libray function _ReadInteger
; Read a interger form the console
; Return the integer that read
;----------------------------------------------------------------------
_ReadInteger proc
push ebp
mov ebp,esp
call _ReadLine
push eax
push eax
call _@atoi
pop ebx
push eax
invoke Free,ebx
pop eax
pop ebp
ret 0
_ReadInteger endp
;----------------------------------------------------------------------
; Standard libray function _Halt
; Halt the program, click OK button to Exit the program
; No return Values
;----------------------------------------------------------------------
_Halt proc near
invoke ExitProcess, NULL
_Halt endp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -