Skip to content

Instantly share code, notes, and snippets.

@dstein64
Last active October 29, 2017 03:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstein64/eda0edbdce6c83f8495bcd057daad57c to your computer and use it in GitHub Desktop.
Save dstein64/eda0edbdce6c83f8495bcd057daad57c to your computer and use it in GitHub Desktop.
Helper macro and function for printenv.s and echo.s
.section .text
# ************************************
# * print macro
# * Caller is responsible for setting
# * %ecx and %edx, and saving %eax and
# * %ebx if necessary.
# ************************************
.macro print
movl $4, %eax
movl $1, %ebx
int $0x80
.endm
.section .text
# ************************************
# * int strlen(char* str);
# * Returns the length of a string.
# ************************************
.type strlen, @function
strlen:
pushl %ebp
movl %esp, %ebp
movl $0, %eax # Index
movl 8(%ebp), %ecx # Address of str
strlen_loop:
movb (%ecx,%eax,1), %dl # Current char
cmpb $0, %dl
je strlen_end
incl %eax
jmp strlen_loop
strlen_end:
movl %ebp, %esp
popl %ebp
ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment