Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 13 files changed
  • 0 comments
  • 1 contributor
Apr 18, 2012
Yaşar Arabacı Add shared library to make file aa31f9a
Yaşar Arabacı Implement trivial programs using assembly
Rewrite
* true
* false
* cat
Using assembly. Compile small assembly library
for often used system calls.
ccbc428
13 Makefile
@@ -14,8 +14,10 @@ libdir = $(prefix)/lib
14 14 syslibdir = /lib
15 15
16 16 LIB_BASE_NAME = ycoreutils
17   -LIB_NAME = lib$(LIB_BASE_NAME).a
18   -LIBRARY = lib/$(LIB_NAME)
  17 +STATIC_LIB_NAME = lib$(LIB_BASE_NAME).a
  18 +SHARED_LIB_NAME = lib$(LIB_BASE_NAME).so
  19 +STATIC_LIBRARY = lib/$(STATIC_LIB_NAME)
  20 +SHARED_LIBRARY = lib/$(SHARED_LIB_NAME)
19 21 LIBRARY_SRCS = $(sort $(wildcard lib/*.c))
20 22 LIBRARY_OBJS = $(LIBRARY_SRCS:.c=.o)
21 23
@@ -35,7 +37,7 @@ RANLIB = ranlib
35 37
36 38 -include config.mak
37 39
38   -all: $(LIBRARY) $(BINARIES)
  40 +all: $(SHARED_LIBRARY) $(STATIC_LIBRARY) $(BINARIES)
39 41
40 42 install : $(DESTDIR)$(libdir)/$(LIB_NAME) $(INCLUDES:include/%=$(DESTDIR)$(includedir)/%) $(BINARY_NAMES:%=$(DESTDIR)$(bindir)/%)
41 43
@@ -51,11 +53,14 @@ clean:
51 53 strip: $(BINARIES)
52 54 strip $^
53 55
54   -$(LIBRARY): $(LIBRARY_OBJS) $(INCLUDES)
  56 +$(STATIC_LIBRARY): $(LIBRARY_OBJS) $(INCLUDES)
55 57 rm -f $@
56 58 $(AR) rc $@ $(LIBRARY_OBJS)
57 59 $(RANLIB) $@
58 60
  61 +$(SHARED_LIBRARY): $(LIBRARY_OBJS) $(INCLUDES)
  62 + ld -shared $(LIBRARY_OBJS) -o $@
  63 +
59 64 %.o: %.c
60 65 $(CC) $(CFLAGS) $(INC) -c -o $@ $<
61 66
19 asm/Makefile
... ... @@ -0,0 +1,19 @@
  1 +SOURCES = $(wildcard *.s)
  2 +OBJECTS = $(SOURCES:%.s=%.o)
  3 +BINS = $(SOURCES:%.s=%)
  4 +LD = ld
  5 +AS = as
  6 +CC = gcc
  7 +CFLAGS = -static -nostdlib
  8 +ASFLAGS = --gstabs
  9 +
  10 +all: $(BINS)
  11 +%: %.s
  12 +%: %.o lib/libsyscalls.a
  13 + ld $< ./lib/libsyscalls.a -o $@
  14 +strip: $(BINS)
  15 + strip $^
  16 +clean:
  17 + rm -f $(OBJECTS)
  18 + rm -f $(BINS)
  19 +.PRECIOUS: $(OBJECTS)
60 asm/cat.s
... ... @@ -0,0 +1,60 @@
  1 +.include "linux.inc"
  2 +.section .bss
  3 + .equ B_SIZE, 512
  4 + .lcomm BUFFER, B_SIZE
  5 +.section .text
  6 +.globl _start
  7 +_start:
  8 + movl %esp, %ebp
  9 + subl $4, %esp # save spot for fd
  10 + movl $2, %edi
  11 +loop_begin:
  12 + cmpl (%ebp), %edi
  13 + jg loop_end
  14 + movl (%ebp, %edi, 4), %eax # save pointer on eax
  15 + cmpb $'-', (%eax) # if doesn't start with - open it
  16 + jne open_file
  17 + cmpb $0, 1(%eax)
  18 + jne open_file
  19 + movl $STDIN, -4(%ebp)
  20 + jmp read_write_loop
  21 +open_file:
  22 + pushl $0666
  23 + pushl $0 # readonly
  24 + pushl %eax
  25 + call open
  26 + addl $12, %esp
  27 + cmpl $0, %eax
  28 + jl error_exit
  29 + movl %eax, -4(%ebp)
  30 +read_write_loop:
  31 + pushl $B_SIZE
  32 + pushl $BUFFER
  33 + pushl -4(%ebp)
  34 + call read
  35 + addl $12, %esp # restore stack
  36 + pushl %eax
  37 + pushl $BUFFER
  38 + pushl $STDOUT
  39 + call write
  40 + addl $8, %esp
  41 + popl %eax
  42 + cmpl $B_SIZE, %eax # didn't read enough
  43 + jl end_read_write_loop
  44 + jmp read_write_loop
  45 +end_read_write_loop:
  46 + cmpl $STDIN, -4(%ebp)
  47 + je continue # continue if reading from stdin
  48 + pushl -4(%ebp)
  49 + call close
  50 + addl $4, %esp
  51 +continue:
  52 + incl %edi
  53 + jmp loop_begin
  54 +loop_end:
  55 + pushl $0
  56 + call exit
  57 +
  58 +error_exit:
  59 + pushl %eax
  60 + call exit
7 asm/false.s
... ... @@ -0,0 +1,7 @@
  1 +.include "linux.inc"
  2 +.section .text
  3 +.globl _start
  4 +_start:
  5 + movl $SYS_EXIT, %eax
  6 + movl $1 , %ebx
  7 + int $LINUX_SYSCALL
18 asm/lib/Makefile
... ... @@ -0,0 +1,18 @@
  1 +AR = ar
  2 +RANLIB = ranlib
  3 +SOURCES = $(wildcard *.s)
  4 +OBJECTS = $(SOURCES:%.s=%.o)
  5 +
  6 +ASFLAGS = --gstabs
  7 +
  8 +all: libsyscalls.a libsyscalls.so
  9 +
  10 +libsyscalls.a: $(OBJECTS)
  11 + rm -f $@
  12 + $(AR) rc $@ $^
  13 + $(RANLIB) $@
  14 +
  15 +libsyscalls.so: $(OBJECTS)
  16 + ld -shared $^ -o $@
  17 +
  18 +.PRECIOUS: $(OBJECTS)
11 asm/lib/close.s
... ... @@ -0,0 +1,11 @@
  1 +.include "../linux.inc"
  2 +.section .text
  3 +.globl close
  4 +.type close, @function
  5 +# close a file
  6 +# argument: file descriptor
  7 +close:
  8 + movl 4(%esp), %ebx
  9 + movl $SYS_CLOSE, %eax
  10 + int $LINUX_SYSCALL
  11 + ret
11 asm/lib/exit.s
... ... @@ -0,0 +1,11 @@
  1 +.include "../linux.inc"
  2 +.section .text
  3 +.globl exit
  4 +.type exit, @function
  5 +# Exit with status code
  6 +# argument: status code
  7 +exit:
  8 + movl 4(%esp), %ebx
  9 + movl $SYS_EXIT, %eax
  10 + int $LINUX_SYSCALL
  11 + ret
15 asm/lib/open.s
... ... @@ -0,0 +1,15 @@
  1 +.include "../linux.inc"
  2 +.section .text
  3 +.globl open
  4 +.type open, @function
  5 +# Open a file
  6 +# argument: address of filename
  7 +# argument: flags
  8 +# argument: mode
  9 +open:
  10 + movl 12(%esp), %edx
  11 + movl 8(%esp), %ecx
  12 + movl 4(%esp), %ebx
  13 + movl $SYS_OPEN, %eax
  14 + int $LINUX_SYSCALL
  15 + ret
15 asm/lib/read.s
... ... @@ -0,0 +1,15 @@
  1 +.include "../linux.inc"
  2 +.section .text
  3 +.globl read
  4 +.type read, @function
  5 +# Read a file into a buffer
  6 +# argument: file descriptor
  7 +# argument: buffer address
  8 +# argument: size
  9 +read:
  10 + movl 12(%esp), %edx
  11 + movl 8(%esp), %ecx
  12 + movl 4(%esp), %ebx
  13 + movl $SYS_READ, %eax
  14 + int $LINUX_SYSCALL
  15 + ret
15 asm/lib/write.s
... ... @@ -0,0 +1,15 @@
  1 +.include "../linux.inc"
  2 +.section .text
  3 +.globl write
  4 +.type write, @function
  5 +# Write buffer to a file
  6 +# argument: file descriptor
  7 +# argument: buffer address
  8 +# argument: count
  9 +write:
  10 + movl 12(%esp), %edx
  11 + movl 8(%esp), %ecx
  12 + movl 4(%esp), %ebx
  13 + movl $SYS_WRITE, %eax
  14 + int $LINUX_SYSCALL
  15 + ret
20 asm/linux.inc
... ... @@ -0,0 +1,20 @@
  1 +# Common Linux Definitions
  2 +
  3 +# System Call Numbers
  4 + .equ SYS_EXIT, 1
  5 + .equ SYS_READ, 3
  6 + .equ SYS_WRITE, 4
  7 + .equ SYS_OPEN, 5
  8 + .equ SYS_CLOSE, 6
  9 + .equ SYS_BRK, 45
  10 +
  11 +# System Call Interrupt Number
  12 + .equ LINUX_SYSCALL, 0x80
  13 +
  14 +# Standard File Descriptors
  15 + .equ STDIN, 0
  16 + .equ STDOUT, 1
  17 + .equ STDERR, 2
  18 +
  19 +# Common Status Codes
  20 + .equ EOF, 0
7 asm/true.s
... ... @@ -0,0 +1,7 @@
  1 +.include "linux.inc"
  2 +.section .text
  3 +.globl _start
  4 +_start:
  5 + movl $SYS_EXIT, %eax
  6 + movl $0 , %ebx
  7 + int $LINUX_SYSCALL
2  src/mkdir.c
@@ -33,7 +33,7 @@ int main(int argc, char *argv[])
33 33 default:
34 34 abort ();
35 35 }
36   -
  36 + // Handle other command line arguments
37 37 for (i = optind; i < argc; ++i) {
38 38 errno = 0;
39 39 if (parent) {

No commit comments for this range

Something went wrong with that request. Please try again.