Created
December 31, 2014 08:37
-
-
Save ultimape/0e413627eea05b660039 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; ========================================================================= | |
; PROGRAMMER : Nicholas Perry | |
; FILE : str_lib.asm | |
; DATE : 2005-09-06 | |
; SUBJECT : lab#4 | |
; my personal library of string functions I made to help myself in micro course | |
; ========================================================================= | |
; Declare the C file handling functions. | |
EXTERN _printf | |
EXTERN _getchb | |
EXTERN _putch | |
GLOBAL String_Length | |
GLOBAL String_Copy | |
GlOBAL String_Cat | |
GLOBAL String_Search | |
CR EQU 13 | |
LF EQU 10 | |
;--------------------------------- | |
; Data Segment | |
;--------------------------------- | |
SECTION .data | |
;; Declare needed variables here. | |
copy_eax DD 0 ; This file pointer is used to access the | |
copy_ebx DD 0 ; This file pointer is used to access the | |
copy_ecx DD 0 ; This file pointer is used to access the | |
copy_edx DD 0 ; This file pointer is used to access the | |
copy_esi DD 0 ; This file pointer is used to access the | |
copy_edi DD 0 ; This file pointer is used to access the | |
;------------------------------------------- | |
; Block Storage Segment | |
;------------------------------------------- | |
SECTION .bss | |
;; These should be the only uninitialized buffers you need. | |
;--------------------------------- | |
; Code Segment | |
;--------------------------------- | |
SECTION .text | |
String_Length | |
;; Returns number of characters in a null | |
;; terminated string pointed to by esi. | |
;; Return Location: eax | |
;; Does not change any other registers | |
;; Copy out registers, just in case | |
push esi | |
mov eax, DWORD 0 | |
lenfindloop: | |
cmp [esi], BYTE 0 ; See if cur char is null. | |
je endlenfindloop ; If is, stop searching. | |
inc eax ; else, count as char. | |
inc esi ; Goto next letter | |
jmp lenfindloop ; Repeat | |
endlenfindloop: | |
pop esi | |
ret | |
String_Copy | |
;; Copys the null terminated string found | |
;; as a refrence from esi, to the refrence | |
;; from edi | |
;; Returns the number of bytes copied | |
;; not including null char. | |
;; Return Location eax | |
;; Does not change any other registers | |
;; Copy out registers, just in case | |
push ecx | |
push ebx | |
push edi | |
push esi | |
;; Find string length | |
push esi | |
call String_Length | |
pop esi | |
mov ecx,eax ; Move string length to counter. | |
mov eax,DWORD 0 ; Start Byte count at 0 | |
stringcopyloop: | |
mov bl,BYTE [esi] ; Move letter from loc esi to loc esi. | |
mov [edi], bl | |
inc eax ; Count letter | |
inc edi ; Next letter | |
inc esi | |
loop stringcopyloop ; keep going till at end | |
mov [edi],DWORD 0 ; Place null char. | |
pop esi | |
pop edi | |
pop ebx | |
pop ecx | |
ret | |
String_Cat | |
;; Concatanates null terminated string | |
;; in esi onto the end of edi | |
;; Returns the number of bytes copied, | |
;; not including null char into eax | |
;; Copy out registers, just in case | |
push ecx | |
push ebx | |
push edi | |
push esi | |
;; Find end of destination | |
mov esi, edi | |
call String_Length | |
add edi, eax | |
pop esi ; restore esi to previous source | |
push esi | |
;; Find string length to copy | |
push esi | |
call String_Length | |
pop esi | |
mov ecx,eax ; Move string length to counter. | |
mov eax,DWORD 0 ; Start Byte count at 0 | |
stringcatloop: | |
mov bl,BYTE [esi] ; Move letter from loc esi to loc esi. | |
mov [edi], bl | |
inc eax ; Count letter | |
inc edi ; Next letter | |
inc esi | |
loop stringcatloop ; keep going till at end | |
mov [edi],DWORD 0 ; Place null char. | |
pop esi | |
pop edi | |
pop ebx | |
pop ecx | |
ret | |
String_Search | |
;; Searches null terminated string | |
;; from esi, looking for char in al, | |
;; Returns a pointer to that char, | |
;; or Null pointer if not found, into eax | |
;; Copy out registers, just in case | |
push ebx | |
push ecx | |
push edi | |
push eax | |
mov edi, esi ; SCASB uses edi, not esi... | |
call String_Length ; Find length of string | |
mov ecx, eax ; store length as counter | |
inc ecx | |
mov ebx, eax ; store length | |
pop eax ; restore eax to letter | |
cld ; auto increment? | |
REPNE SCASB ; keep going while not found al in edi | |
cmp ecx, DWORD 0 | |
je notfound | |
;; length - ecx = offset, | |
sub ebx, ecx ; Find position of letter, if found | |
mov eax, ebx ; move to return value | |
add eax, esi ; change offset to pointer | |
jmp found | |
notfound: | |
xor eax,eax ; I'm an uber1337 assembler monkey. | |
found: | |
pop edi | |
pop ecx | |
pop ebx | |
ret |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment