sample code for dorks like me
I dunno asm that well... I wrote this that gives the system uptime. I had to copy the functions for outputting to console though. It's from some random website on the web. Only the stuff in MAIN and outputUptime is mine :(
In case anyone wants it:
Code:
TITLE Windows Uptime
.386
.MODEL flat,stdcall
.STACK 4096
option casemap:none
STD_OUTPUT_HANDLE EQU -11 ; Win32 Console handle
WriteConsole EQU <WriteConsoleA>
ExitProcess PROTO, dwExitCode:DWORD
GetTickCount PROTO
GetStdHandle PROTO, nStdHandle:DWORD
WriteConsole PROTO, handle:DWORD,
lpBuffer:PTR BYTE,
nNumberOfCharsToWrite:DWORD,
lpNumberOfCharsWritten:PTR DWORD,
lpReserved:DWORD
.data
consoleOutHandle DWORD ?
bytesWritten DWORD ?
InitFlag BYTE 0 ; initialization flag
days DWORD 0
hours DWORD 0
mins DWORD 0
secs DWORD 0
divisors DWORD 86400000, ; days
3600000, ; hours
60000, ; minutes
1000 ; seconds
strMsg BYTE "Windows uptime: ",0
strDays BYTE " day(s), ", 0
strHours BYTE " hour(s), ", 0
strMins BYTE " minute(s), ", 0
strSecs BYTE " and second(s)", 0
.code
main PROC
INVOKE GetTickCount
mov edx, 0 ; clear the dividend, high
div divisors[0] ; divide to get the DAYS
mov days, eax
mov eax, edx ; save the remainder of division
mov edx, 0
div divisors[4] ; divide to get the HOURS
mov hours, eax
mov eax, edx
mov edx, 0
div divisors[8] ; divide to get the MINUTES
mov mins, eax
mov eax, edx
mov edx, 0
div divisors[12] ; divide to get the SECONDS
mov secs, eax
call outputUptime
INVOKE ExitProcess, 0
main ENDP
outputUptime PROC
INVOKE GetStdHandle, STD_OUTPUT_HANDLE
mov consoleOutHandle, eax
mov edx, OFFSET strMsg
call WriteString
mov eax, days
call WriteInt
mov edx, OFFSET strDays
call WriteString
mov eax, hours
call WriteInt
mov edx, OFFSET strHours
call WriteString
mov eax, mins
call WriteInt
mov edx, OFFSET strMins
call WriteString
mov eax, secs
call WriteInt
mov edx, OFFSET strSecs
call WriteString
ret
outputUptime ENDP
;----------------------------------------------------
Initialize PROC PRIVATE
;
; Get the standard console handles for input and output,
; and set a flag indicating that it has been done.
;----------------------------------------------------
pushad
INVOKE GetStdHandle, STD_OUTPUT_HANDLE
mov [consoleOutHandle],eax
mov InitFlag,1
popad
ret
Initialize ENDP
;----------------------------------------------------
NewLine MACRO
; Send a newline sequence to standard output
;----------------------------------------------------
LOCAL temp
.data
temp BYTE 13,10,0
.code
pushfd
push edx
mov edx,OFFSET temp
call WriteString
pop edx
popfd
ENDM
;---------------------------------------------------------
Str_length PROC USES edi,
pString:PTR BYTE ; pointer to string
;
; Return the length of a null-terminated string.
; Receives: pString - pointer to a string
; Returns: EAX = string length
; Last update: 1/18/02
;---------------------------------------------------------
mov edi,pString
mov eax,0 ; character count
L1:
cmp byte ptr [edi],0 ; end of string?
je L2 ; yes: quit
inc edi ; no: point to next
inc eax ; add 1 to count
jmp L1
L2: ret
Str_length ENDP
;---------------------------------------------------
; Check to see if the console handles have been initialized
CheckInit MACRO
;----------------------------------------------------
LOCAL exit
cmp InitFlag,0
jne exit
call Initialize
exit:
ENDM
;-----------------------------------------------------
WriteInt proc
;
; Writes a 32-bit signed binary integer to standard output
; in ASCII decimal.
; Receives: EAX = the integer
; Returns: nothing
; Comments: Displays a leading sign, no leading zeros.
; Last update: 7/11/01
;-----------------------------------------------------
WI_Bufsize = 12
true = 1
false = 0
.data
buffer_B db WI_Bufsize dup(0),0 ; buffer to hold digits
neg_flag db ?
.code
pushad
CheckInit
mov neg_flag,false ; assume neg_flag is false
or eax,eax ; is AX positive?
jns WIS1 ; yes: jump to B1
neg eax ; no: make it positive
mov neg_flag,true ; set neg_flag to true
WIS1:
mov ecx,0 ; digit count = 0
mov edi,offset buffer_B
add edi,(WI_Bufsize-1)
mov ebx,10 ; will divide by 10
WIS2:
mov edx,0 ; set dividend to 0
div ebx ; divide AX by 10
or dl,30h ; convert remainder to ASCII
dec edi ; reverse through the buffer
mov [edi],dl ; store ASCII digit
inc ecx ; increment digit count
or eax,eax ; quotient > 0?
jnz WIS2 ; yes: divide again
; Insert the sign.
cmp neg_flag,false ; was the number positive?
jz WIS3 ; yes
dec edi ; back up in the buffer
inc ecx ; increment counter
;mov byte ptr [edi],'+' ; insert plus sign
mov byte ptr [edi],'-' ; no: insert negative sign
WIS3: ; Display the number
mov edx,edi
call WriteString
popad
ret
WriteInt ENDP
;-----------------------------------------------------
Crlf proc
;
; Writes a carriage return / linefeed
; sequence (0Dh,0Ah) to standard output.
;-----------------------------------------------------
CheckInit
NewLine ; invoke a macro
ret
Crlf ENDP
;--------------------------------------------------------
WriteString proc
;
; Writes a null-terminated string to standard
; output. Input parameter: EDX points to the
; string.
; Last update: 9/7/01
;--------------------------------------------------------
pushad
CheckInit
INVOKE Str_length,edx ; return length of string in EAX
cld ; must do this before WriteConsole
INVOKE WriteConsole,
consoleOutHandle, ; console output handle
edx, ; points to string
eax, ; string length
offset bytesWritten, ; returns number of bytes written
0
popad
ret
WriteString ENDP
END main