/* * Lookup a primitive within a given table * * to compile: `as lookup.s -o lookup.o; ld lookup.o -o lookup' * * to execute: `./lookup; echo $?' * * returns `1' if found, `0' if not * */ .data /* data section */ /* primitives' lookup table */ primitives: .ascii "input", "erasechar", "quit", "bye", "exit", "\0" /* primitive to match */ ptest: .asciz "bye" ptest_len = .- ptest-1 .text /* text section */ .globl _start .type _start, @function /* main */ _start: pushl $ptest call LookUp addl $4, %esp movl %eax, %ebx /* exit */ movl $1, %eax int $0x80 .type LookUp, @function /* lookup function */ .globl LookUp LookUp: pushl %ebp movl %esp, %ebp movl 0x8(%ebp), %ebx # pointer to string to match movl $primitives, %ecx # primitives' buffer movb (%ebx, 1), %dl # store %bl into %dl .LookUp_search: cmpb $0x0, (%ecx,1) # watch out for end of buffer je .LookUp_done cmpb %dl, (%ecx,1) # compare first pointer char with current ecx je .LookUp_match incl %ecx # increment ecx jmp .LookUp_search .LookUp_match: pushl %ebx # save & send ebx as func's arg pushl %ecx # save & send ecx as func's arg call StrCmp popl %ecx # pop it popl %ebx # pop it incl %ecx # increment ecx cmpl $0x1, %eax # check StrCmp's result je .LookUp_done jmp .LookUp_search .LookUp_done: movl %ebp, %esp popl %ebp ret .type StrCmp, @function /* strcmp function */ .globl StrCmp StrCmp: pushl %ebp movl %esp, %ebp movl 0x8(%ebp), %esi # pointer to primitive's buffer movl 0xc(%ebp), %edi # pointer to the string to match movl $ptest_len, %ecx # requested primitive's len cld repe cmpsb jg .StrCmp_no movl $1,%eax jmp .StrCmp_end .StrCmp_no: movl $0,%eax .StrCmp_end: movl %ebp, %esp popl %ebp ret