main.asm

URL: https://mirkwood.cs.edinboro.edu/~bennett/class/cmsc3100/spring2026/notes/files/code/orderedList/main.asm
 
%include "CONSTANTS.h"

O_RDONLY equ 0
SYS_OPEN equ 2
SYS_CLOSE equ 3

extern malloc
extern free

extern GetString
extern GET_WORD
extern PRINT_LISTR
extern PRINT_LIST
extern NEW_NODE
extern INSERT_NODE
extern DELETE_LIST

section .data
     FileName: db `ocap.txt`,0
     fd: dq 0
     head: dq 0
      
section .bss

section .text

global main
main:

    ; open the file
    mov rax, SYS_OPEN
    mov rdi, FileName
    mov rsi, O_RDONLY
    syscall

    mov [fd], rax

.top:

    ; grab a word from the file
    mov rdi, [fd]
    call GET_WORD

    ; save the address of the word.
    mov r12, rax

    ; if we got a 0 back, we are at the end of file.
    cmp rax, 0
    je .READ_done

    ; rax holds the address of the string, and it is not  null
    mov rdi, rax
    call NEW_NODE


    ; rax holds the new node
    ; insert this node
    mov rdi, [head]
    mov rsi, rax
    call INSERT_NODE
    mov qword[head], rax

    ; save the new head pointer
    mov [head], rax

    jmp .top

.READ_done:

    mov rdi, [head]
    call PRINT_LISTR

    mov rdi, [head]

done:
    mov rdi, [head]
    call DELETE_LIST

    mov rax, SYS_CLOSE
    mov rdi, [fd] 
    syscall

    jmp Exit