swissChili | 2999dd1 | 2021-07-02 14:19:53 -0700 | [diff] [blame] | 1 | ;;; TODO: figure out if I need to do something special with the GC here. |
| 2 | |
| 3 | [bits 32] |
| 4 | [global _call_list] |
| 5 | [extern length] |
| 6 | [extern elt] |
| 7 | ;;; This function should call it's first argument with the arguments from |
| 8 | ;;; the cons-list passed as its second argument. |
| 9 | |
| 10 | ;;; _call_list(function pointer, cons list) |
| 11 | _call_list: |
| 12 | push ebp |
| 13 | mov ebp, esp |
| 14 | |
| 15 | mov edi, [ebp + 12] ; Cons list |
| 16 | |
| 17 | push edi |
| 18 | call length ; Length of cons list in eax |
| 19 | add esp, 4 |
| 20 | |
| 21 | mov ecx, eax ; Store length in counter |
| 22 | |
| 23 | ;; Reserve space for all the stack items |
| 24 | shl eax, 2 |
| 25 | sub esp, eax |
| 26 | |
| 27 | mov esi, esp ; Pointer to top of stack |
| 28 | |
| 29 | ;; Skip all of this if there are no arguments |
| 30 | cmp ecx, 0 |
| 31 | je .done |
| 32 | |
| 33 | .loop: |
| 34 | ;; Get the previous item. At the start ecx = the length so to get the last |
| 35 | ;; index we need to subtract 1 |
| 36 | dec ecx |
| 37 | |
| 38 | push ecx |
| 39 | push edi |
| 40 | call elt |
| 41 | add esp, 4 |
| 42 | pop ecx ; This is a scratch register, remember |
| 43 | |
| 44 | ;; We now have the ecx-th item in eax |
| 45 | ;; Remember esi is the top of the stack area reserved, so |
| 46 | mov [esi + 4 * ecx], eax |
| 47 | |
| 48 | jcxz .done |
| 49 | jmp .loop |
| 50 | |
| 51 | .done: |
| 52 | mov ebx, [ebp + 8] ; Function pointer |
| 53 | call ebx |
| 54 | |
| 55 | mov esp, ebp |
| 56 | pop ebp |
| 57 | ret |