Something like this?
bits 32
%ifdef TESTMAIN
section .data
section .bss
buf resb 80
section .data
filename db `l_gets.asm\0` ; ourself - we know it's there
section .text
global _start
_start:
mov eax, 5 ; sys_open
mov ebx, filename
xor ecx, ecx
xor edx, edx
int 80h
test eax, eax
js exit ; bail out if error
; test a call to it
push 80 ; length
push buf
push eax ; fd
call l_gets
add esp, 4 * 3
; find NL - we want to stop there
xor edx, edx
find:
cmp [ecx + edx], byte 10
je found
inc edx
jmp find
found:
; edx is length to print
;print what we l_getsed
; mov edx, eax ; length read
mov eax, 4 ;sys_write
mov ebx, 1 ; stdout
mov ecx, buf
int 80h
exit:
mov eax, 1 ; sys exit
mov ebx, edx ; return value is number of bytes written (len)
int 0x80
%endif
;-----------------------------
global l_gets
l_gets:
push ebp ; prologue, set up stack frame
mov ebp, esp
; xor eax, eax ; zero eax to prepare for syscall #
push ebx ; preserve ebx
mov ebx, [ebp + 8] ; fd parameter goes into ebx
mov ecx, [ebp + 12] ; char *buf stored into ecx
mov edx, [ebp + 16] ; len stored into edx
cmp edx, 0 ; if len is zero or less, exit program
jle .done
; read data onto stack:
.buf_loop:
; push esp
; cmp esp, byte 0x0A ; if a newline character is encountered, exit
; je .done
; sub esp, 4 ; will use esp for the pointer to the buffer, the bytes to be read will be pushed onto stack
mov eax, 3 ; sys call for read, to begin reading bytes- for sys read, ebx= int (fd), ecx= char, edx= size_t (len)
int 0x80
; read each character one at a time, increment counter (in eax), when counter matches len, jump out of loop
; inc eax ; advance the counter
; cmp edx, eax
; je .done
; jmp .buf_loop ; continue
.done:
pop ebx ; restore caller's reg
mov esp, ebp
pop ebp
ret
Could put the "find linefeed" part in the function. That may be what the assignment expects?
Best,
Frank
Ahhh, just read your latest. I think your code will work reading from stdin. (ahhh, no it won't - ecx is not likely 0xA - especially before the sys_read!) This tests from a real file. As I expected, it reads past the linefeed. Comment out the "find LF" part to see what it does without it...