1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| $>cat run; echo '---main.c---' && cat main.c && echo '---my_memset.S--' && cat my_memset.S
#!/bin/sh
gcc -c main.c -g3 && nasm -f elf64 -o tmp.o -g3 my_memset.S && gcc main.o tmp.o && ./a.out && rm -f tmp.o main.o
---main.c---
#include <stdio.h>
#include <stdlib.h>
void *my_memset(void *s, int c, size_t n);
int main(int ac, char **av)
{
int *tab;
int idx;
tab = malloc(sizeof(*tab) * 10);
idx = 0;
while (idx < 10)
tab[idx++] = idx;
printf("tab = %p\n", tab);
idx = -1;
while (++idx < 10)
printf("tab[%i] = %i\n", idx, tab[idx]);
printf("my_memset(tab, 42, 10) = %p\n", my_memset(tab, 42, 10));
idx = -1;
while (++idx < 10)
printf("tab[%i] = %i\n", idx, tab[idx]);
return 0;
}
---my_memset.S--
[BITS 64]
global my_memset
section .text
; void* memset(void *s, int c, size_t n)
; rdi rsi rdx
my_memset:
push rbp ; prologue
mov rbp, rsp
xor rcx, rcx ; compteur
.while:
cmp rcx, rdx ; while rcx != rdx
je .endwhile
mov [rdi+rcx], rsi ; remplissage
inc rcx ; incrementation du compteur
jmp .while
.endwhile:
mov rax, rdi ; valeur de retour
mov rsp,rbp ; epilogue
pop rbp
ret |
Partager