Assembly

Scott Lanning slanning at buphy.bu.edu
Sun Jan 30 10:43:22 EST 2000


On Sat, 29 Jan 2000, Derek Martin wrote:
>This might be a good time to point out that the actual purpose of
>the cat command is not to display a file, but to conCATenate
>multiple files together.

okay, so I made my assembly cat do concatenation of several
files or accept stdin. It bloated from 444 bytes to 452. :)
Only thing it lacks that I can see is command-line flags,
but I've never used them for cat, so....

as cat.s -o cat.o
ld cat.o -o cat
strip cat

(don't forget to use as ./cat)

####
	BUF_LEN  = 8192
	O_RDONLY = 0

	.lcomm	buf, BUF_LEN

.text
	.globl _start		#entry point declared for linker (ld)
_start:
	movl	$0, %ebp	#default stdin if no args

        pop     %ecx		#pop argument count
        dec     %ecx		#is there anything on command line?
        pop     %ecx		#skip name of program		
        jz      .read		#no, use stdin
.next:
	pop	%ebx		#pop filename
	or	%ebx, %ebx
	jz	exit		#exit if no more args

	movl	$5, %eax	#sys_open
	movl	$O_RDONLY, %ecx	#mode
	int	$0x80		#call sys_open

	movl	%eax, %ebp	#fd returned from open()
	
	test	%eax,%eax	#have we opened file?
	jns	.read		#yes, read it


	jmp	exit

.read:
	movl	$buf, %ecx	#buffer

.loop:
	movl	$3, %eax	#sys_read
	movl	%ebp, %ebx	#fd
	movl	$BUF_LEN, %edx	#buffer length
	int	$0x80		#call sys_read

	test	%eax, %eax
	js	exit
	jz	.next

	movl	%eax, %edx
	movl	$4, %eax	#sys_write
	movl	$1, %ebx	#fd == 1 == stdout
	int	$0x80

	jmp	.loop
exit:
	movl	$1, %eax
	movl	$0, %ebx
	int	$0x80

-
Subcription/unsubscription/info requests: send e-mail with
"subscribe", "unsubscribe", or "info" on the first line of the
message body to discuss-request at blu.org (Subject line is ignored).



More information about the Discuss mailing list