Browse Source

Added code and slides from 2019-05-10 OpenLab

Matteo Savatteri 5 years ago
parent
commit
34f431a149

BIN
code/asm/file/a.out


+ 34 - 0
code/asm/file/list

@@ -0,0 +1,34 @@
+     1                                  SEGMENT .text
+     2                                  GLOBAL _start
+     3                                  _start:
+     4 00000000 B802000000              	MOV RAX, 2		; open(filename, flags)
+     5 00000005 48BF-                   	MOV RDI, filename
+     5 00000007 [0000000000000000] 
+     6 0000000F 4831F6                  	XOR RSI, RSI		; set flags to 0: RD_ONLY
+     7 00000012 0F05                    	SYSCALL
+     8 00000014 4883F8FF                	CMP RAX, -1
+     9 00000018 7433                    	JE err
+    10 0000001A 4889C7                  	MOV RDI, RAX		; file descriptor
+    11 0000001D 4831C0                  	XOR RAX, RAX		; read(fd, buf, count)
+    12 00000020 48BE-                   	MOV RSI, buf
+    12 00000022 [0000000000000000] 
+    13 0000002A BA13000000              	MOV RDX, 19		; We read at most 19 bytes
+    14 0000002F 0F05                    	SYSCALL
+    15 00000031 4883F8FF                	CMP RAX, -1
+    16 00000035 7416                    	JE err
+    17 00000037 B801000000              	MOV RAX, 1		; write(fd, buf) (buf is already set from previous call)
+    18 0000003C BF01000000              	MOV RDI, 1		; standard output
+    19 00000041 0F05                    	SYSCALL
+    20 00000043 B83C000000              	MOV RAX, 60		; exit(code)
+    21 00000048 4831FF                  	XOR RDI, RDI		; success
+    22 0000004B 0F05                    	SYSCALL
+    23                                  err:
+    24 0000004D B83C000000              	MOV RAX, 60		; exit(code)
+    25 00000052 BF01000000              	MOV RDI, 1		; 1 (fail)
+    26 00000057 0F05                    	SYSCALL
+    27                                  	
+    28                                  SEGMENT .bss
+    29 00000000 <res 00000014>          buf RESB 20
+    30                                  
+    31                                  SEGMENT .rodata
+    32 00000000 66696C6500              filename DB "file",0

BIN
code/asm/file/main.o


+ 32 - 0
code/asm/file/main.s

@@ -0,0 +1,32 @@
+SEGMENT .text
+GLOBAL _start
+_start:
+	MOV RAX, 2		; open(filename, flags)
+	MOV RDI, filename
+	XOR RSI, RSI		; set flags to 0: RD_ONLY
+	SYSCALL
+	CMP RAX, -1
+	JE err
+	MOV RDI, RAX		; file descriptor
+	XOR RAX, RAX		; read(fd, buf, count)
+	MOV RSI, buf
+	MOV RDX, 19		; We read at most 19 bytes
+	SYSCALL
+	CMP RAX, -1
+	JE err
+	MOV RAX, 1		; write(fd, buf) (buf is already set from previous call)
+	MOV RDI, 1		; standard output
+	SYSCALL
+	MOV RAX, 60		; exit(code)
+	XOR RDI, RDI		; success
+	SYSCALL
+err:
+	MOV RAX, 60		; exit(code)
+	MOV RDI, 1		; 1 (fail)
+	SYSCALL
+	
+SEGMENT .bss
+buf RESB 20
+
+SEGMENT .rodata
+filename DB "file",0

BIN
code/asm/hello/a.out


BIN
code/asm/hello/main.o


+ 14 - 0
code/asm/hello/main.s

@@ -0,0 +1,14 @@
+SEGMENT .text
+GLOBAL _start
+_start:
+	MOV RAX, 1
+	MOV RDI, RAX
+	MOV RSI, hello
+	MOV RDX, 13
+	SYSCALL
+	MOV RAX, 60
+	XOR RDI, RDI
+	SYSCALL
+
+SEGMENT .rodata
+hello DB `hello, world\n`,0

+ 0 - 0
arch → code/c/arch


+ 18 - 0
code/c/arch.c

@@ -0,0 +1,18 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(void)
+{
+	int fd = creat(".BTW_I_USE_ARCH", 0664);
+	if (fd == -1) {
+        	puts("YOU SHOULD USE ARCH");
+		exit(1);
+	}
+	puts("BTW, I USE ARCH");
+	close(fd);
+	exit(0);
+}

+ 0 - 0
mem → code/c/mem


+ 30 - 0
code/c/mem.c

@@ -0,0 +1,30 @@
+#include <malloc.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	/* Apre il file con il nome con cui e' stato invocato questo programma */
+	int f = open(argv[0], O_RDONLY);
+	struct stat s;
+	int *buf;
+
+	if (f == -1) {
+		fprintf(stderr, "Errore apertura file\n");
+		return 1;
+	}
+	if (fstat(f, &s) == -1) {
+		fprintf(stderr, "Errore fstat\n");
+		return 2;
+	}
+	if (!(buf = malloc(s.st_size))) {
+		fprintf(stderr, "Errore malloc\n");
+		return 3;
+	}
+	read(f, buf, s.st_size);
+	free(buf);
+	close(f);
+	return 0;
+}

BIN
slides/fs_cd_2019-05-10.pdf.xz