bltn_cons.asm 915 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. %include "syscalls.asm"
  2. %include "print_type.asm"
  3. %include "heap.asm"
  4. %include "type_check.asm"
  5. section .text
  6. bltn_cons:
  7. ;; INPUT: rax, r8
  8. ;; output: (cons rax r8) in the rax register
  9. ;; save rax
  10. ;; [ASSUMPTION: heap_alloc does not trash r9]
  11. mov r9, rax
  12. ;; allocate heap space
  13. mov rax, 2
  14. call heap_alloc
  15. ;; put the CAR in
  16. mov [rax], r9
  17. ;; put the CDR in
  18. mov [rax + 8], r8
  19. ;; create a cons cell object to return
  20. or rax, 0b010
  21. ret
  22. bltn_car:
  23. ;; INPUT: rax
  24. ;; OUTPUT: car(rax)
  25. ;; ensure that it is a cons
  26. mov r8, 0b010
  27. call type_check
  28. ;; extract the pointer
  29. mov rcx, rax
  30. and rcx, 0xFFFFFFFFFFFFFFFC
  31. ;; deref it
  32. mov rax, [rcx]
  33. ret
  34. bltn_cdr:
  35. ;; INPUT: rax
  36. ;; OUTPUT: cdr(rax)
  37. ;; ensure that it is a cons
  38. mov r8, 0b010
  39. call type_check
  40. ;; extract the pointer
  41. mov rcx, rax
  42. and rcx, 0xFFFFFFFFFFFFFFFC
  43. ;; inc
  44. add rcx, 8
  45. ;; deref it
  46. mov rax, [rcx]
  47. ret