guest54
Guest
|
 |
« on: December 18, 2011, 03:01:38 pm » |
|
; Simple examples of the use of 64-bit assembly language to perform ; a few fundamental functions in a "c" and "windows" environment.
; ====================================================================
extern printf extern _getch extern gets extern _findfirst64 extern _findnext64 extern _findclose extern fopen extern fread extern feof extern fclose extern GetCommandLineA
; ====================================================================
buflen equ 2000000 ; File input buffer size
; ====================================================================
; Structure definition used by findfirst64 and findnext64:
struc finddata64_t attrib: resq 1 time_create: resq 1 time_access: resq 1 time_write: resq 1 size: resq 1 name: resb 260 endstruc
; ====================================================================
; This directive is essential; 64-bit code normally addresses things ; in a new mode, relative to the instruction pointer!
default rel
; ====================================================================
section .code
[BITS 64] global _start
; The conventional prologue _start: mov [rsp + 8],rcx push r15 push r14 push r13 sub rsp,128 ; Stack space
; Get the command line (an example of one of the many hundreds of ; Windows APIs which may be called in this way) xor rcx,rcx xor rdx,rdx xor r8,r8 xor r9,r9 call GetCommandLineA ; Returns rax pointing to the command line
; Display something using printf mov rcx,mess1 mov rdx,rax ; Pointer to the command line (%s) xor r8,r8 xor r9,r9 call printf
; Get some text from the user (note - no check for buffer overflow here - to avoid ; that one could use GetStdHandle and fgets, which accepts a maximum count) mov rcx,buffer xor rdx,rdx xor r8,r8 xor r9,r9 call gets ; mov rcx,youent mov rdx,buffer xor r8,r8 xor r9,r9 call printf
; List a selection of files in the current directory mov rcx,wci ; The mask *.txt will select all text files mov rdx,finddata64 ; Address of the result structure xor r8,r8 xor r9,r9 call _findfirst64 ; cmp rax,-1 je goout ; --> No file of that kind found mov [hfile],rax ; Save the file "handle"
; Display the file's name and size filope: mov rcx,fileinfo mov rdx,finddata64+name ; Address of the file name mov r8,[finddata64+size] ; File size - note that it is a quadword here (%lld) xor r9,r9 call printf
; Open the file for reading in binary mode mov rcx,finddata64+name ; Address of the file name mov rdx,ipmods ; Mode string - important - must be zero-terminated! xor r8,r8 xor r9,r9 call fopen
; It will return either a FILE * or null or rax,rax jz error ; --> Open failed mov qword [filpoi],rax ; Save the FILE pointer
; Test whether end of file has been reached mov rcx,rax ; The FILE pointer xor rdx,rdx xor r8,r8 xor r9,r9 call feof ; or rax,rax jnz goteof ; --> Yes it has
; Read the first twenty bytes of the file mov rcx,buffer ; Destination mov rdx,1 ; unit or item mov r8,20 ; Read 20 bytes mov r9,qword [filpoi] ; FILE * call fread ; or rax,rax jz goteof ; --> No readable bytes
; Display those 20 bytes in hexadecimal. Note that printf will ; preserve registers r13 and r14 mov r13,buffer ; First byte to be displayed mov r14,buffer+20 disbak: mov rcx,bytesmes xor rdx,rdx mov dl,[r13] xor r8,r8 xor r9,r9 call printf inc r13 cmp r13,r14 jne disbak ; mov rcx,bmafter xor rdx,rdx xor r8,r8 xor r9,r9 call printf
; Close the file goteof: mov rcx,[filpoi] xor rdx,rdx xor r8,r8 xor r9,r9 call fclose
; Wait for a key-press - if the user types "x" we exit right away xor rcx,rcx call _getch cmp al,'x' je goout
; Otherwise we go on to find the next file, if there is one mov rcx,[hfile] ; The findfirst handle again mov rdx,finddata64 xor r8,r8 xor r9,r9 call _findnext64 ; or rax,rax jz filope ; --> Found one, so go back and open it ; jmp goout ; --> No more files found
; ---------------------------------------------------
; Error exit error: mov rcx,errmes xor rdx,rdx xor r8,r8 xor r9,r9 call printf
; Normal exit ; Call _findclose if necessary to terminate the directory loop goout: mov rcx,[hfile] or rcx,rcx jz noneed xor rdx,rdx xor r8,r8 xor r9,r9 call _findclose noneed:
; Standard epilogue xor rax,rax ; add rsp,128 pop r13 pop r14 pop r15 ret
; ====================================================================
section .data
wci db "*.txt",0 ; Wild-card will find all .txt files ipmods db "rb",0 ; Modes for file binary read
align 16 filpoi dq 0 ; FILE pointer hfile dq 0 ; Handle from findfirst
; An instance of the structure finddata64_t. The initializations (the lines ; beginning with "at") are not always necessary. finddata64: istruc finddata64_t at attrib, dq 0 at time_create, dq 0 at time_access, dq 0 at time_write, dq 0 at size, dq 0 at name, db 0 iend
errmes db "Error",0 mess1 db 'Command line is ->%s<-',0x0a,0x0d, 'Please type your name: ',0 youent db 'You entered "%s"',0x0a,0x0a,0x0d,0 fileinfo db 'File name "%s," file size %lld',0x0d,0x0a,0 bytesmes db '%2.2x ',0 bmafter db 0x0d,0x0a, 0x0d,0x0a, 0x07,0 ; The 7 is a proper beep
; ====================================================================
section .bss
; Input buffer alignb 16 buffer: resb buflen
; End of the programme ===============================================
|