; list.asm a direct coding of list.c (list.c included as comments) ; standard function entry and exit used, not optimized ; Compile: nasm -f elf list.asm ; link: gcc -o test_list_str test_list_str.c list.o ; run: test_list_str ; Author: Spencer Shimko (Portions of code by Jon Squire) ; list.c this or however you wish, is the functions of list.asm ; #include ; extern printf in list.asm extern printf ; static char heap[20000] ; space to store strings, do not reuse or free ; static char *hp = heap; ; pointer to next available heap space ; static int list[1000]; ; space to store list block (2 index+ pointer) ; static int lp=1; ; index to next available list space ; static char *q; ; a variable pointer ; static int i; ; a variable index section .data hp: dd heap lp: dd 1 fmt1: db "%s",10,0 section .bss heap: resb 20000 list: resd 1000 q: resd 1 i: resd 1 ; +-----------------+ +-----------------+ +-----------------+ ; L[0]-> | index to next----->| index to next----->| 0 | ; | 0 |<-----index to prev |<-----index to prev |<-L[1] ; | ptr to heap str | | ptr to heap str | | ptr to heap str | ; +-----------------+ +-----------------+ +-----------------+ ; The pointers to heap strings are character pointes to terminated strings. ; The "index" values couls be pointers rather than indices. ; void clear(int *L) ; initialize front and back pointers to zero ; { section .text global clear clear: push ebp ; save ebp for later mov ebp, esp ; ebp is callers stack push ebx ; save registers push edi push esi ; L[0]=0; ; later, will be index into "front" of list ; L[1]=0; ; later, will be index into "back" of list mov edi,[ebp+8] mov eax,0 ; get a 32-bit zero mov [edi],eax ; L[0]=0 mov [edi+4],eax ; L[1]=0 pop esi pop edi pop ebx mov esp,ebp pop ebp ret ; } ; void push_front(int *L, char *s) ; { global push_front push_front: push ebp mov ebp, esp push ebx push edi push esi mov edi,[ebp+4*2] mov ecx,0x0 cmp [edi],ecx jne update mov ecx,[lp] mov [edi], ecx mov [edi+4],ecx mov ebx, 0x0 mov [list+4*ecx], ebx mov [list+4*ecx +4], ebx jmp listPnt update: mov ecx, [edi] mov [i],ecx mov ebx, [lp] mov[edi], ebx mov [list+ebx*4], ecx mov ecx, 0x0 mov [list +4*ebx + 4], ecx mov [list +4*ecx +4], ebx listPnt: mov ebx, [lp] mov ecx, [hp] mov [list+4*ebx + 8], ecx add ebx, 3 mov [lp], ebx mov esi,[ebp+12] mov [q], esi mov ecx, [q] mov edx, [ecx] cmp edx, 0x0 je pushdone hashPnt: mov ebx, [hp] mov edx, [ecx] mov [ebx], edx inc ebx mov [hp], ebx inc ecx mov [q], ecx mov edx, [ecx] cmp edx, 0x0 jne hashPnt pushdone: mov ecx, 0x0 mov EAX, [hp] mov [EAX], ecx inc EAX mov [hp], EAX pop esi pop edi pop ebx mov esp,ebp pop ebp ret global pop_back pop_back: push ebp mov ebp, esp push ebx push edi push esi mov edi,[ebp+8] mov ecx,0x0 cmp [edi+4], ecx jne popbackIF ret popbackIF: mov ebx,[edi] mov EAX, [edi+4] cmp EAX ,ebx jne pbLoop2 mov ecx, 0x0 mov [edi],ecx mov [edi+4],ecx ret pbLoop2: mov ecx, [edi+4] mov ecx, [list+4*ecx+4] mov [edi+4], ecx mov ebx,0x0 mov [list+4*ecx], ebx pop esi pop edi pop ebx mov esp,ebp pop ebp ret global print print: push ebp ; save ebp mov ebp, esp ; ebp is callers stack push ebx ; save registers push edi push esi ; i=L[0]; /* index into front of 'list' */ mov edi,[ebp+8] ; get address of L[0] into edi mov edi, [edi] printl: ; while(i) /* keep looping until i==0, meaning end of list */ ; { ; q=(char *)list[i+2]; /* get this list items pointer to string */ mov esi,[list+4*edi+8] push dword esi push dword fmt1 call printf add esp,8 mov edi,[list+4*edi] cmp edi, 0x0 je printd jmp printl ; } printd: pop esi ; restore registers pop edi ; in reverse order pop ebx mov esp,ebp ; restore callers stack frame pop ebp ret ; return ; } global pop_front pop_front: push ebp mov ebp, esp push ebx push edi push esi mov edi,[ebp+8] mov ecx,0x0 cmp [edi], ecx jne LoopCompare ret LoopCompare: mov ecx, [edi+4] cmp [edi],ecx jne replaceLoop mov ecx,0x0 mov [edi],ecx mov [edi+4],ecx ret replaceLoop: mov ecx, [i] ; mov edi,[ebp+8] mov ecx,[edi] mov ecx, [list+4*ecx] mov [edi],ecx mov ebx,0x0 mov [list+4*ecx +4], ebx pop esi pop edi pop ebx mov esp,ebp pop ebp ret ; return global push_back push_back: push ebp ; save ebp mov ebp, esp ; ebp is callers stack push ebx ; save registers push edi push esi mov edi,[ebp+8] ; get address of L into edi mov edx, [edi] ; grab L[0]; cmp edx, 0 jne else2 ; check if list is empty and goto block if (L[0] == 0) if2: mov dword ecx, [lp] ; L[0] = lp; mov dword [edi], ecx mov dword ecx, [lp] ; L[1] = lp; mov dword [edi+4], ecx ; mov dword [list+4*ecx], 0 ; list[lp] = 0; mov dword [list+4*ecx+4], 0 ; list[lp+1] = 0; jmp endif2 else2: mov dword ecx, [edi+4] ; i = L[1]; mov dword [i], ecx mov dword ecx, [lp] ; L[1] = lp; mov dword [edi+4], ecx mov dword EAX, [lp] mov dword [list+4*EAX], 0 ; list[lp]=0; mov dword ecx, [i] ; list[lp+1]=i; mov dword [list+4*EAX+4], ecx mov dword EAX, [i] mov dword ecx, [lp] ; list[i]=lp; mov dword [list+4*(EAX)], ecx endif2: mov ebx, [lp] mov EAX, [hp] mov [list + 4*ebx + 8], EAX ; list[lp + 2] = hp add ebx, 3 mov [lp], ebx ; lp = lp + 3 mov esi, [ebp+12] mov [q], esi ; q = s mov EAX, [q] mov edx, [EAX] cmp edx, 0 ; while *q isn't 0 start loop je end_w2 cpyhp2: mov ebx, [hp] mov edx, [EAX] mov [ebx], edx ; *hp = *q inc ebx mov [hp], ebx ; hp++ inc EAX mov [q], EAX ; q++ mov edx, [EAX] cmp edx, 0 ; while *q isn't 0 loop again jne cpyhp2 mov EAX, 0 mov ecx, [hp] mov [ecx], EAX inc ecx mov [hp], ecx end_w2: mov EAX, [hp] mov dword [EAX], 0 ; inc EAX mov [hp], EAX pop esi ; restore registers pop edi ; in reverse order pop ebx mov esp,ebp ; restore callers stack frame pop ebp ret ; return ; }