summaryrefslogtreecommitdiff
path: root/CSAPP
diff options
context:
space:
mode:
authorCarlos Maiolino <[email protected]>2025-07-10 22:55:07 +0200
committerCarlos Maiolino <[email protected]>2025-07-10 22:56:55 +0200
commitd98f46ce647846b0aa30b2e16a30fd4e152a1bf5 (patch)
tree267474fcc77cf20b428f6f4c7f768ca09f4cfe0e /CSAPP
parent869e68986aa8f69af6e7842260a68d1e5c6f796f (diff)
Add new code
Signed-off-by: Carlos Maiolino <[email protected]>
Diffstat (limited to 'CSAPP')
-rw-r--r--CSAPP/NOTES.txt12
-rw-r--r--CSAPP/chap1/hello_compilation_steps/hello.c7
-rw-r--r--CSAPP/chap1/hello_compilation_steps/hello_asm_phase2.s26
-rw-r--r--CSAPP/chap1/hello_compilation_steps/hello_post_preprocessor.i801
-rw-r--r--CSAPP/chap2/2_12.c12
-rw-r--r--CSAPP/chap2/2_26.c9
-rw-r--r--CSAPP/chap2/bittruncate.c9
-rw-r--r--CSAPP/chap2/casting.c36
-rw-r--r--CSAPP/chap2/char_endian.c6
-rw-r--r--CSAPP/chap2/datatype.c15
-rw-r--r--CSAPP/chap2/detect_overflow.c54
-rw-r--r--CSAPP/chap2/encoding.c25
-rw-r--r--CSAPP/chap2/endianess.c17
-rw-r--r--CSAPP/chap2/hello.c7
-rw-r--r--CSAPP/chap2/not.c14
-rw-r--r--CSAPP/chap2/shift_big.c40
-rw-r--r--CSAPP/chap2/shifts.c39
-rw-r--r--CSAPP/chap2/show_types.c11
-rw-r--r--CSAPP/chap2/string.c11
-rw-r--r--CSAPP/chap3/main.c17
-rw-r--r--CSAPP/chap3/mstore.c7
-rw-r--r--CSAPP/chap3/mstore.s21
-rw-r--r--CSAPP/chap3/mstore_intel.S22
-rw-r--r--CSAPP/dsizes.c12
-rw-r--r--CSAPP/leaq.c4
-rw-r--r--CSAPP/leaq.s35
-rw-r--r--CSAPP/shift.c7
-rw-r--r--CSAPP/shift.s17
-rw-r--r--CSAPP/tmul.c14
-rw-r--r--CSAPP/tmul.s43
-rw-r--r--CSAPP/tmul_asm.c51
-rw-r--r--CSAPP/tmul_asm.s79
-rw-r--r--CSAPP/xor.c12
33 files changed, 1492 insertions, 0 deletions
diff --git a/CSAPP/NOTES.txt b/CSAPP/NOTES.txt
new file mode 100644
index 0000000..05cb653
--- /dev/null
+++ b/CSAPP/NOTES.txt
@@ -0,0 +1,12 @@
+#########################################################
+# Computer Systems - A programmer's perspective #
+# #
+# Personal notes #
+# #
+#########################################################
+
+
+-= Chapter 1 =-
+
+
+
diff --git a/CSAPP/chap1/hello_compilation_steps/hello.c b/CSAPP/chap1/hello_compilation_steps/hello.c
new file mode 100644
index 0000000..2581101
--- /dev/null
+++ b/CSAPP/chap1/hello_compilation_steps/hello.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int main(void)
+{
+ printf("Hello world\n");
+ return 0;
+}
diff --git a/CSAPP/chap1/hello_compilation_steps/hello_asm_phase2.s b/CSAPP/chap1/hello_compilation_steps/hello_asm_phase2.s
new file mode 100644
index 0000000..08014a3
--- /dev/null
+++ b/CSAPP/chap1/hello_compilation_steps/hello_asm_phase2.s
@@ -0,0 +1,26 @@
+ .file "hello.c"
+ .section .rodata
+.LC0:
+ .string "Hello world"
+ .text
+ .globl main
+ .type main, @function
+main:
+.LFB0:
+ .cfi_startproc
+ pushq %rbp
+ .cfi_def_cfa_offset 16
+ .cfi_offset 6, -16
+ movq %rsp, %rbp
+ .cfi_def_cfa_register 6
+ movl $.LC0, %edi
+ call puts
+ movl $0, %eax
+ popq %rbp
+ .cfi_def_cfa 7, 8
+ ret
+ .cfi_endproc
+.LFE0:
+ .size main, .-main
+ .ident "GCC: (GNU) 7.2.1 20170915 (Red Hat 7.2.1-2)"
+ .section .note.GNU-stack,"",@progbits
diff --git a/CSAPP/chap1/hello_compilation_steps/hello_post_preprocessor.i b/CSAPP/chap1/hello_compilation_steps/hello_post_preprocessor.i
new file mode 100644
index 0000000..38966c9
--- /dev/null
+++ b/CSAPP/chap1/hello_compilation_steps/hello_post_preprocessor.i
@@ -0,0 +1,801 @@
+# 1 "./hello.c"
+# 1 "<built-in>"
+# 1 "<command-line>"
+# 31 "<command-line>"
+# 1 "/usr/include/stdc-predef.h" 1 3 4
+# 32 "<command-line>" 2
+# 1 "./hello.c"
+# 1 "/usr/include/stdio.h" 1 3 4
+# 27 "/usr/include/stdio.h" 3 4
+# 1 "/usr/include/bits/libc-header-start.h" 1 3 4
+# 33 "/usr/include/bits/libc-header-start.h" 3 4
+# 1 "/usr/include/features.h" 1 3 4
+# 423 "/usr/include/features.h" 3 4
+# 1 "/usr/include/sys/cdefs.h" 1 3 4
+# 419 "/usr/include/sys/cdefs.h" 3 4
+# 1 "/usr/include/bits/wordsize.h" 1 3 4
+# 420 "/usr/include/sys/cdefs.h" 2 3 4
+# 1 "/usr/include/bits/long-double.h" 1 3 4
+# 421 "/usr/include/sys/cdefs.h" 2 3 4
+# 424 "/usr/include/features.h" 2 3 4
+# 447 "/usr/include/features.h" 3 4
+# 1 "/usr/include/gnu/stubs.h" 1 3 4
+# 10 "/usr/include/gnu/stubs.h" 3 4
+# 1 "/usr/include/gnu/stubs-64.h" 1 3 4
+# 11 "/usr/include/gnu/stubs.h" 2 3 4
+# 448 "/usr/include/features.h" 2 3 4
+# 34 "/usr/include/bits/libc-header-start.h" 2 3 4
+# 28 "/usr/include/stdio.h" 2 3 4
+
+
+
+
+
+# 1 "/usr/lib/gcc/x86_64-redhat-linux/7/include/stddef.h" 1 3 4
+# 216 "/usr/lib/gcc/x86_64-redhat-linux/7/include/stddef.h" 3 4
+
+# 216 "/usr/lib/gcc/x86_64-redhat-linux/7/include/stddef.h" 3 4
+typedef long unsigned int size_t;
+# 34 "/usr/include/stdio.h" 2 3 4
+
+# 1 "/usr/include/bits/types.h" 1 3 4
+# 27 "/usr/include/bits/types.h" 3 4
+# 1 "/usr/include/bits/wordsize.h" 1 3 4
+# 28 "/usr/include/bits/types.h" 2 3 4
+
+
+typedef unsigned char __u_char;
+typedef unsigned short int __u_short;
+typedef unsigned int __u_int;
+typedef unsigned long int __u_long;
+
+
+typedef signed char __int8_t;
+typedef unsigned char __uint8_t;
+typedef signed short int __int16_t;
+typedef unsigned short int __uint16_t;
+typedef signed int __int32_t;
+typedef unsigned int __uint32_t;
+
+typedef signed long int __int64_t;
+typedef unsigned long int __uint64_t;
+
+
+
+
+
+
+
+typedef long int __quad_t;
+typedef unsigned long int __u_quad_t;
+
+
+
+
+
+
+
+typedef long int __intmax_t;
+typedef unsigned long int __uintmax_t;
+# 130 "/usr/include/bits/types.h" 3 4
+# 1 "/usr/include/bits/typesizes.h" 1 3 4
+# 131 "/usr/include/bits/types.h" 2 3 4
+
+
+typedef unsigned long int __dev_t;
+typedef unsigned int __uid_t;
+typedef unsigned int __gid_t;
+typedef unsigned long int __ino_t;
+typedef unsigned long int __ino64_t;
+typedef unsigned int __mode_t;
+typedef unsigned long int __nlink_t;
+typedef long int __off_t;
+typedef long int __off64_t;
+typedef int __pid_t;
+typedef struct { int __val[2]; } __fsid_t;
+typedef long int __clock_t;
+typedef unsigned long int __rlim_t;
+typedef unsigned long int __rlim64_t;
+typedef unsigned int __id_t;
+typedef long int __time_t;
+typedef unsigned int __useconds_t;
+typedef long int __suseconds_t;
+
+typedef int __daddr_t;
+typedef int __key_t;
+
+
+typedef int __clockid_t;
+
+
+typedef void * __timer_t;
+
+
+typedef long int __blksize_t;
+
+
+
+
+typedef long int __blkcnt_t;
+typedef long int __blkcnt64_t;
+
+
+typedef unsigned long int __fsblkcnt_t;
+typedef unsigned long int __fsblkcnt64_t;
+
+
+typedef unsigned long int __fsfilcnt_t;
+typedef unsigned long int __fsfilcnt64_t;
+
+
+typedef long int __fsword_t;
+
+typedef long int __ssize_t;
+
+
+typedef long int __syscall_slong_t;
+
+typedef unsigned long int __syscall_ulong_t;
+
+
+
+typedef __off64_t __loff_t;
+typedef __quad_t *__qaddr_t;
+typedef char *__caddr_t;
+
+
+typedef long int __intptr_t;
+
+
+typedef unsigned int __socklen_t;
+
+
+
+
+typedef int __sig_atomic_t;
+# 36 "/usr/include/stdio.h" 2 3 4
+# 1 "/usr/include/bits/types/__FILE.h" 1 3 4
+
+
+
+struct _IO_FILE;
+typedef struct _IO_FILE __FILE;
+# 37 "/usr/include/stdio.h" 2 3 4
+# 1 "/usr/include/bits/types/FILE.h" 1 3 4
+
+
+
+struct _IO_FILE;
+
+
+typedef struct _IO_FILE FILE;
+# 38 "/usr/include/stdio.h" 2 3 4
+
+
+
+# 1 "/usr/include/libio.h" 1 3 4
+# 31 "/usr/include/libio.h" 3 4
+# 1 "/usr/include/_G_config.h" 1 3 4
+# 15 "/usr/include/_G_config.h" 3 4
+# 1 "/usr/lib/gcc/x86_64-redhat-linux/7/include/stddef.h" 1 3 4
+# 16 "/usr/include/_G_config.h" 2 3 4
+
+# 1 "/usr/include/bits/types/__mbstate_t.h" 1 3 4
+# 13 "/usr/include/bits/types/__mbstate_t.h" 3 4
+typedef struct
+{
+ int __count;
+ union
+ {
+ unsigned int __wch;
+ char __wchb[4];
+ } __value;
+} __mbstate_t;
+# 18 "/usr/include/_G_config.h" 2 3 4
+
+
+
+
+typedef struct
+{
+ __off_t __pos;
+ __mbstate_t __state;
+} _G_fpos_t;
+typedef struct
+{
+ __off64_t __pos;
+ __mbstate_t __state;
+} _G_fpos64_t;
+# 32 "/usr/include/libio.h" 2 3 4
+# 49 "/usr/include/libio.h" 3 4
+# 1 "/usr/lib/gcc/x86_64-redhat-linux/7/include/stdarg.h" 1 3 4
+# 40 "/usr/lib/gcc/x86_64-redhat-linux/7/include/stdarg.h" 3 4
+typedef __builtin_va_list __gnuc_va_list;
+# 50 "/usr/include/libio.h" 2 3 4
+# 145 "/usr/include/libio.h" 3 4
+struct _IO_jump_t; struct _IO_FILE;
+
+
+
+
+typedef void _IO_lock_t;
+
+
+
+
+
+struct _IO_marker {
+ struct _IO_marker *_next;
+ struct _IO_FILE *_sbuf;
+
+
+
+ int _pos;
+# 173 "/usr/include/libio.h" 3 4
+};
+
+
+enum __codecvt_result
+{
+ __codecvt_ok,
+ __codecvt_partial,
+ __codecvt_error,
+ __codecvt_noconv
+};
+# 241 "/usr/include/libio.h" 3 4
+struct _IO_FILE {
+ int _flags;
+
+
+
+
+ char* _IO_read_ptr;
+ char* _IO_read_end;
+ char* _IO_read_base;
+ char* _IO_write_base;
+ char* _IO_write_ptr;
+ char* _IO_write_end;
+ char* _IO_buf_base;
+ char* _IO_buf_end;
+
+ char *_IO_save_base;
+ char *_IO_backup_base;
+ char *_IO_save_end;
+
+ struct _IO_marker *_markers;
+
+ struct _IO_FILE *_chain;
+
+ int _fileno;
+
+
+
+ int _flags2;
+
+ __off_t _old_offset;
+
+
+
+ unsigned short _cur_column;
+ signed char _vtable_offset;
+ char _shortbuf[1];
+
+
+
+ _IO_lock_t *_lock;
+# 289 "/usr/include/libio.h" 3 4
+ __off64_t _offset;
+
+
+
+
+
+
+
+ void *__pad1;
+ void *__pad2;
+ void *__pad3;
+ void *__pad4;
+
+ size_t __pad5;
+ int _mode;
+
+ char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
+
+};
+
+
+typedef struct _IO_FILE _IO_FILE;
+
+
+struct _IO_FILE_plus;
+
+extern struct _IO_FILE_plus _IO_2_1_stdin_;
+extern struct _IO_FILE_plus _IO_2_1_stdout_;
+extern struct _IO_FILE_plus _IO_2_1_stderr_;
+# 333 "/usr/include/libio.h" 3 4
+typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes);
+
+
+
+
+
+
+
+typedef __ssize_t __io_write_fn (void *__cookie, const char *__buf,
+ size_t __n);
+
+
+
+
+
+
+
+typedef int __io_seek_fn (void *__cookie, __off64_t *__pos, int __w);
+
+
+typedef int __io_close_fn (void *__cookie);
+# 385 "/usr/include/libio.h" 3 4
+extern int __underflow (_IO_FILE *);
+extern int __uflow (_IO_FILE *);
+extern int __overflow (_IO_FILE *, int);
+# 429 "/usr/include/libio.h" 3 4
+extern int _IO_getc (_IO_FILE *__fp);
+extern int _IO_putc (int __c, _IO_FILE *__fp);
+extern int _IO_feof (_IO_FILE *__fp) __attribute__ ((__nothrow__ , __leaf__));
+extern int _IO_ferror (_IO_FILE *__fp) __attribute__ ((__nothrow__ , __leaf__));
+
+extern int _IO_peekc_locked (_IO_FILE *__fp);
+
+
+
+
+
+extern void _IO_flockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__));
+extern void _IO_funlockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__));
+extern int _IO_ftrylockfile (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__));
+# 458 "/usr/include/libio.h" 3 4
+extern int _IO_vfscanf (_IO_FILE * __restrict, const char * __restrict,
+ __gnuc_va_list, int *__restrict);
+extern int _IO_vfprintf (_IO_FILE *__restrict, const char *__restrict,
+ __gnuc_va_list);
+extern __ssize_t _IO_padn (_IO_FILE *, int, __ssize_t);
+extern size_t _IO_sgetn (_IO_FILE *, void *, size_t);
+
+extern __off64_t _IO_seekoff (_IO_FILE *, __off64_t, int, int);
+extern __off64_t _IO_seekpos (_IO_FILE *, __off64_t, int);
+
+extern void _IO_free_backup_area (_IO_FILE *) __attribute__ ((__nothrow__ , __leaf__));
+# 42 "/usr/include/stdio.h" 2 3 4
+
+
+
+
+typedef __gnuc_va_list va_list;
+# 57 "/usr/include/stdio.h" 3 4
+typedef __off_t off_t;
+# 71 "/usr/include/stdio.h" 3 4
+typedef __ssize_t ssize_t;
+
+
+
+
+
+
+typedef _G_fpos_t fpos_t;
+# 131 "/usr/include/stdio.h" 3 4
+# 1 "/usr/include/bits/stdio_lim.h" 1 3 4
+# 132 "/usr/include/stdio.h" 2 3 4
+
+
+
+extern struct _IO_FILE *stdin;
+extern struct _IO_FILE *stdout;
+extern struct _IO_FILE *stderr;
+
+
+
+
+
+
+extern int remove (const char *__filename) __attribute__ ((__nothrow__ , __leaf__));
+
+extern int rename (const char *__old, const char *__new) __attribute__ ((__nothrow__ , __leaf__));
+
+
+
+extern int renameat (int __oldfd, const char *__old, int __newfd,
+ const char *__new) __attribute__ ((__nothrow__ , __leaf__));
+
+
+
+
+
+
+
+extern FILE *tmpfile (void) ;
+# 173 "/usr/include/stdio.h" 3 4
+extern char *tmpnam (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ;
+
+
+
+
+extern char *tmpnam_r (char *__s) __attribute__ ((__nothrow__ , __leaf__)) ;
+# 190 "/usr/include/stdio.h" 3 4
+extern char *tempnam (const char *__dir, const char *__pfx)
+ __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__malloc__)) ;
+
+
+
+
+
+
+
+extern int fclose (FILE *__stream);
+
+
+
+
+extern int fflush (FILE *__stream);
+# 213 "/usr/include/stdio.h" 3 4
+extern int fflush_unlocked (FILE *__stream);
+# 232 "/usr/include/stdio.h" 3 4
+extern FILE *fopen (const char *__restrict __filename,
+ const char *__restrict __modes) ;
+
+
+
+
+extern FILE *freopen (const char *__restrict __filename,
+ const char *__restrict __modes,
+ FILE *__restrict __stream) ;
+# 265 "/usr/include/stdio.h" 3 4
+extern FILE *fdopen (int __fd, const char *__modes) __attribute__ ((__nothrow__ , __leaf__)) ;
+# 278 "/usr/include/stdio.h" 3 4
+extern FILE *fmemopen (void *__s, size_t __len, const char *__modes)
+ __attribute__ ((__nothrow__ , __leaf__)) ;
+
+
+
+
+extern FILE *open_memstream (char **__bufloc, size_t *__sizeloc) __attribute__ ((__nothrow__ , __leaf__)) ;
+
+
+
+
+
+extern void setbuf (FILE *__restrict __stream, char *__restrict __buf) __attribute__ ((__nothrow__ , __leaf__));
+
+
+
+extern int setvbuf (FILE *__restrict __stream, char *__restrict __buf,
+ int __modes, size_t __n) __attribute__ ((__nothrow__ , __leaf__));
+
+
+
+
+extern void setbuffer (FILE *__restrict __stream, char *__restrict __buf,
+ size_t __size) __attribute__ ((__nothrow__ , __leaf__));
+
+
+extern void setlinebuf (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
+
+
+
+
+
+
+
+extern int fprintf (FILE *__restrict __stream,
+ const char *__restrict __format, ...);
+
+
+
+
+extern int printf (const char *__restrict __format, ...);
+
+extern int sprintf (char *__restrict __s,
+ const char *__restrict __format, ...) __attribute__ ((__nothrow__));
+
+
+
+
+
+extern int vfprintf (FILE *__restrict __s, const char *__restrict __format,
+ __gnuc_va_list __arg);
+
+
+
+
+extern int vprintf (const char *__restrict __format, __gnuc_va_list __arg);
+
+extern int vsprintf (char *__restrict __s, const char *__restrict __format,
+ __gnuc_va_list __arg) __attribute__ ((__nothrow__));
+
+
+
+extern int snprintf (char *__restrict __s, size_t __maxlen,
+ const char *__restrict __format, ...)
+ __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 4)));
+
+extern int vsnprintf (char *__restrict __s, size_t __maxlen,
+ const char *__restrict __format, __gnuc_va_list __arg)
+ __attribute__ ((__nothrow__)) __attribute__ ((__format__ (__printf__, 3, 0)));
+# 365 "/usr/include/stdio.h" 3 4
+extern int vdprintf (int __fd, const char *__restrict __fmt,
+ __gnuc_va_list __arg)
+ __attribute__ ((__format__ (__printf__, 2, 0)));
+extern int dprintf (int __fd, const char *__restrict __fmt, ...)
+ __attribute__ ((__format__ (__printf__, 2, 3)));
+
+
+
+
+
+
+
+extern int fscanf (FILE *__restrict __stream,
+ const char *__restrict __format, ...) ;
+
+
+
+
+extern int scanf (const char *__restrict __format, ...) ;
+
+extern int sscanf (const char *__restrict __s,
+ const char *__restrict __format, ...) __attribute__ ((__nothrow__ , __leaf__));
+# 395 "/usr/include/stdio.h" 3 4
+extern int fscanf (FILE *__restrict __stream, const char *__restrict __format, ...) __asm__ ("" "__isoc99_fscanf")
+
+ ;
+extern int scanf (const char *__restrict __format, ...) __asm__ ("" "__isoc99_scanf")
+ ;
+extern int sscanf (const char *__restrict __s, const char *__restrict __format, ...) __asm__ ("" "__isoc99_sscanf") __attribute__ ((__nothrow__ , __leaf__))
+
+ ;
+# 420 "/usr/include/stdio.h" 3 4
+extern int vfscanf (FILE *__restrict __s, const char *__restrict __format,
+ __gnuc_va_list __arg)
+ __attribute__ ((__format__ (__scanf__, 2, 0))) ;
+
+
+
+
+
+extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg)
+ __attribute__ ((__format__ (__scanf__, 1, 0))) ;
+
+
+extern int vsscanf (const char *__restrict __s,
+ const char *__restrict __format, __gnuc_va_list __arg)
+ __attribute__ ((__nothrow__ , __leaf__)) __attribute__ ((__format__ (__scanf__, 2, 0)));
+# 443 "/usr/include/stdio.h" 3 4
+extern int vfscanf (FILE *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vfscanf")
+
+
+
+ __attribute__ ((__format__ (__scanf__, 2, 0))) ;
+extern int vscanf (const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vscanf")
+
+ __attribute__ ((__format__ (__scanf__, 1, 0))) ;
+extern int vsscanf (const char *__restrict __s, const char *__restrict __format, __gnuc_va_list __arg) __asm__ ("" "__isoc99_vsscanf") __attribute__ ((__nothrow__ , __leaf__))
+
+
+
+ __attribute__ ((__format__ (__scanf__, 2, 0)));
+# 477 "/usr/include/stdio.h" 3 4
+extern int fgetc (FILE *__stream);
+extern int getc (FILE *__stream);
+
+
+
+
+
+extern int getchar (void);
+# 495 "/usr/include/stdio.h" 3 4
+extern int getc_unlocked (FILE *__stream);
+extern int getchar_unlocked (void);
+# 506 "/usr/include/stdio.h" 3 4
+extern int fgetc_unlocked (FILE *__stream);
+# 517 "/usr/include/stdio.h" 3 4
+extern int fputc (int __c, FILE *__stream);
+extern int putc (int __c, FILE *__stream);
+
+
+
+
+
+extern int putchar (int __c);
+# 537 "/usr/include/stdio.h" 3 4
+extern int fputc_unlocked (int __c, FILE *__stream);
+
+
+
+
+
+
+
+extern int putc_unlocked (int __c, FILE *__stream);
+extern int putchar_unlocked (int __c);
+
+
+
+
+
+
+extern int getw (FILE *__stream);
+
+
+extern int putw (int __w, FILE *__stream);
+
+
+
+
+
+
+
+extern char *fgets (char *__restrict __s, int __n, FILE *__restrict __stream)
+ ;
+# 603 "/usr/include/stdio.h" 3 4
+extern __ssize_t __getdelim (char **__restrict __lineptr,
+ size_t *__restrict __n, int __delimiter,
+ FILE *__restrict __stream) ;
+extern __ssize_t getdelim (char **__restrict __lineptr,
+ size_t *__restrict __n, int __delimiter,
+ FILE *__restrict __stream) ;
+
+
+
+
+
+
+
+extern __ssize_t getline (char **__restrict __lineptr,
+ size_t *__restrict __n,
+ FILE *__restrict __stream) ;
+
+
+
+
+
+
+
+extern int fputs (const char *__restrict __s, FILE *__restrict __stream);
+
+
+
+
+
+extern int puts (const char *__s);
+
+
+
+
+
+
+extern int ungetc (int __c, FILE *__stream);
+
+
+
+
+
+
+extern size_t fread (void *__restrict __ptr, size_t __size,
+ size_t __n, FILE *__restrict __stream) ;
+
+
+
+
+extern size_t fwrite (const void *__restrict __ptr, size_t __size,
+ size_t __n, FILE *__restrict __s);
+# 673 "/usr/include/stdio.h" 3 4
+extern size_t fread_unlocked (void *__restrict __ptr, size_t __size,
+ size_t __n, FILE *__restrict __stream) ;
+extern size_t fwrite_unlocked (const void *__restrict __ptr, size_t __size,
+ size_t __n, FILE *__restrict __stream);
+
+
+
+
+
+
+
+extern int fseek (FILE *__stream, long int __off, int __whence);
+
+
+
+
+extern long int ftell (FILE *__stream) ;
+
+
+
+
+extern void rewind (FILE *__stream);
+# 707 "/usr/include/stdio.h" 3 4
+extern int fseeko (FILE *__stream, __off_t __off, int __whence);
+
+
+
+
+extern __off_t ftello (FILE *__stream) ;
+# 731 "/usr/include/stdio.h" 3 4
+extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);
+
+
+
+
+extern int fsetpos (FILE *__stream, const fpos_t *__pos);
+# 757 "/usr/include/stdio.h" 3 4
+extern void clearerr (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
+
+extern int feof (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
+
+extern int ferror (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
+
+
+
+extern void clearerr_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
+extern int feof_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
+extern int ferror_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
+
+
+
+
+
+
+
+extern void perror (const char *__s);
+
+
+
+
+
+# 1 "/usr/include/bits/sys_errlist.h" 1 3 4
+# 26 "/usr/include/bits/sys_errlist.h" 3 4
+extern int sys_nerr;
+extern const char *const sys_errlist[];
+# 782 "/usr/include/stdio.h" 2 3 4
+
+
+
+
+extern int fileno (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
+
+
+
+
+extern int fileno_unlocked (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
+# 800 "/usr/include/stdio.h" 3 4
+extern FILE *popen (const char *__command, const char *__modes) ;
+
+
+
+
+
+extern int pclose (FILE *__stream);
+
+
+
+
+
+extern char *ctermid (char *__s) __attribute__ ((__nothrow__ , __leaf__));
+# 840 "/usr/include/stdio.h" 3 4
+extern void flockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
+
+
+
+extern int ftrylockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__)) ;
+
+
+extern void funlockfile (FILE *__stream) __attribute__ ((__nothrow__ , __leaf__));
+# 868 "/usr/include/stdio.h" 3 4
+
+# 2 "./hello.c" 2
+
+
+# 3 "./hello.c"
+int main(void)
+{
+ printf("Hello world\n");
+ return 0;
+}
diff --git a/CSAPP/chap2/2_12.c b/CSAPP/chap2/2_12.c
new file mode 100644
index 0000000..b7f53ac
--- /dev/null
+++ b/CSAPP/chap2/2_12.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+int main (void) {
+ int a = 0x87654321;
+
+ printf("Ans A: 0x%.8x\n", (a & 0xff));
+ printf("Ans B: 0x%.8x\n", ((~a) & ((~0) & ~0xff)) | (a & 0xff));
+ printf("Ans B2: 0x%.8x\n", ((~0xff) & (~a)) |(a & 0xff));
+ printf("Ans C: 0x%.8x\n", (a | 0xff));
+
+ return 0;
+}
diff --git a/CSAPP/chap2/2_26.c b/CSAPP/chap2/2_26.c
new file mode 100644
index 0000000..15b605a
--- /dev/null
+++ b/CSAPP/chap2/2_26.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+
+int main(void){
+ unsigned a, b;
+ a = 3;
+ b = 5;
+ printf("%d\n", !!(((int)a - (int)b) > 0));
+ return 0;
+}
diff --git a/CSAPP/chap2/bittruncate.c b/CSAPP/chap2/bittruncate.c
new file mode 100644
index 0000000..757a127
--- /dev/null
+++ b/CSAPP/chap2/bittruncate.c
@@ -0,0 +1,9 @@
+#include <stdio.h>
+
+int main(void) {
+
+ int short a = 0xAA3A;
+ printf("0x%X - 0x%X \n", a, (char)a);
+ return 0;
+}
+
diff --git a/CSAPP/chap2/casting.c b/CSAPP/chap2/casting.c
new file mode 100644
index 0000000..5089802
--- /dev/null
+++ b/CSAPP/chap2/casting.c
@@ -0,0 +1,36 @@
+/*
+ * Shows how the conversion between signed and unsigned are made.
+ * Most machines follow the rule that the underlying bit pattern doesn't change.
+ * Let's see
+ */
+
+#include <stdio.h>
+
+/* 10101010
+ * integers are usually considered signed by default unless we append
+ * the suffix U to the number*/
+#define NUM 0xAA
+int x = -1;
+unsigned u = 2147483648;
+
+int main(void) {
+
+ unsigned char a = NUM;
+ printf("Original unsigned number - HEX: 0x%.2X %d\n", a, a);
+ /* there is no formatter to print a single byte integer, printing it
+ * with %d would result in a cast to integer, printing 4 bytes, so let's
+ * print it only in HEX
+ */
+
+ printf("Signed conversion number - HEX: 0x%.2X DEC: %d\n", (char)a, (signed char)a);
+
+ printf("x = %d - x = %u - 0x%X\n", x, x, x);
+ printf("u = %d - u = %u - 0x%X\n", u, u, u);
+
+ /*
+ * When one of the operands in an equation is unsigned, C will cast the
+ * another operand to unsigned too before making the operation
+ */
+ printf("%X\n", -2147483647);
+ return 0;
+}
diff --git a/CSAPP/chap2/char_endian.c b/CSAPP/chap2/char_endian.c
new file mode 100644
index 0000000..0929a88
--- /dev/null
+++ b/CSAPP/chap2/char_endian.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main(void) {
+ char *b = "TOMANOCU";
+ return 0;
+}
diff --git a/CSAPP/chap2/datatype.c b/CSAPP/chap2/datatype.c
new file mode 100644
index 0000000..3e43ef1
--- /dev/null
+++ b/CSAPP/chap2/datatype.c
@@ -0,0 +1,15 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+
+int main(void)
+{
+ int16_t a = 255;
+ int8_t b = 120;
+ uint32_t c = UINT32_MAX;
+
+ printf("A: %" PRId16 "\n", a);
+ printf("B: %" PRId8 "\n", b);
+ printf("C: %" PRIu32 "\n", c);
+ return 0;
+}
diff --git a/CSAPP/chap2/detect_overflow.c b/CSAPP/chap2/detect_overflow.c
new file mode 100644
index 0000000..6badd11
--- /dev/null
+++ b/CSAPP/chap2/detect_overflow.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <sys/types.h>
+#include <stdlib.h>
+#include <limits.h>
+
+/*
+ * Determine whether arguments can be added without overflow
+ * Two's complement
+ *
+ * Returns 1 if arguments can be added without overflow
+ * 0 otherwise
+ */
+int tadd_ok(int x, int y)
+{
+// if ((x > 0 && y > 0) && (x + y) < 0 ||
+// (x < 0 && y < 0) && (x + y) > 0)
+// return 0;
+// else if ((x < 0 && y < 0) && (x + y) > 0)
+// return 0;
+// else
+// return 1;
+
+ /* Negative overflow can also be 0 */
+ return !(((x > 0 && y > 0) && (x + y) < 0) ||
+ ((x < 0 && y < 0) && (x + y) >= 0));
+}
+
+int uadd_ok(unsigned x, unsigned y)
+{
+ return ((x + y) >= x);
+ //return ((x + y) < x) ? 0 : 1;
+}
+
+
+
+int main(void)
+{
+ unsigned a = 5;
+ unsigned b = 0;
+ int X = INT_MIN;
+ int Y = INT_MIN;
+ int ret;
+
+ ret = uadd_ok(a, b);
+ printf("SUM: %u\n", a + b);
+ printf("Return is: %d\n", ret);
+
+ ret = tadd_ok(X, Y);
+ printf("Signed values\n");
+ printf("SUM: %d\n", X + Y);
+ printf("Return is: %d\n", ret);
+ return 0;
+}
+
diff --git a/CSAPP/chap2/encoding.c b/CSAPP/chap2/encoding.c
new file mode 100644
index 0000000..7a3b0c9
--- /dev/null
+++ b/CSAPP/chap2/encoding.c
@@ -0,0 +1,25 @@
+#include <stdio.h>
+
+int main(void)
+{
+ int a = 2809;
+ float b = 2809;
+ unsigned char *p;
+ int i = 0;
+
+ /* print int and float values as an hexadecimal unsigned integer */
+ p = (unsigned char *)&a;
+ printf(" Int: 0x");
+ for (i = 0; i < sizeof(int); i++) {
+ printf("%.2x", p[i]);
+ printf("\n");
+ }
+
+ p = (unsigned char *)&b;
+ printf(" Float: 0x");
+ for (i = 0; i < sizeof(int); i++) {
+ printf("%.2x", p[i]);
+ printf("\n");
+ }
+ return 0;
+}
diff --git a/CSAPP/chap2/endianess.c b/CSAPP/chap2/endianess.c
new file mode 100644
index 0000000..c2c08aa
--- /dev/null
+++ b/CSAPP/chap2/endianess.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+int main(void)
+{
+ unsigned int a = 0xdc3f109a;
+ /* Preferred data type for expressing size of data structures */
+ size_t i;
+ unsigned char *p;
+
+ printf("Current value of variable: %x\n\n", a);
+ for (i = 0, p = (char *)&a; i < sizeof(int); i++) {
+ printf("Address: %p - Value %x\n", p, (unsigned char)*p);
+ p++;
+ }
+
+ return 0;
+}
diff --git a/CSAPP/chap2/hello.c b/CSAPP/chap2/hello.c
new file mode 100644
index 0000000..2581101
--- /dev/null
+++ b/CSAPP/chap2/hello.c
@@ -0,0 +1,7 @@
+#include <stdio.h>
+
+int main(void)
+{
+ printf("Hello world\n");
+ return 0;
+}
diff --git a/CSAPP/chap2/not.c b/CSAPP/chap2/not.c
new file mode 100644
index 0000000..bb17b7a
--- /dev/null
+++ b/CSAPP/chap2/not.c
@@ -0,0 +1,14 @@
+#include <stdio.h>
+
+int main(void)
+{
+ struct foo {
+ unsigned int a : 1;
+ };
+
+ struct foo bar;
+ bar.a = 1;
+ printf("%x\n", bar.a);
+ printf("%x\n", ~bar.a);
+ return 0;
+}
diff --git a/CSAPP/chap2/shift_big.c b/CSAPP/chap2/shift_big.c
new file mode 100644
index 0000000..8a54c56
--- /dev/null
+++ b/CSAPP/chap2/shift_big.c
@@ -0,0 +1,40 @@
+/*
+ * What happens when the shift value is bigger than the amount of bits in the
+ * variable?
+ *
+ * Some machines will only use the lower log2 w bits of the shift amount when
+ * shifting a 2-bit falue.
+ *
+ * For example, shifting a 8-bit value by 10, would make the machine shift the
+ * value for only 2 (a k mod w).
+ *
+ * This is not a rule though. Let's test it.
+ *
+ * Looks like linux GCC on 64bit will always shift the exactly number, and crop
+ * only the exactly number of bits for the data type representation.
+ *
+ * For signed integers, if the shift is done right in the printf, gcc will use a
+ * convert it to a signed 32-bits for the conversion.
+ *
+ * Try change the x in printf on line 32, to use "a << 10" directly.
+ */
+#include <stdio.h>
+
+int main(void)
+{
+ /* 10101001 */
+ char x, a = 0xA9;
+ unsigned char y, b = 0xA9;
+
+
+ printf("Signed Original value: 0x%.2x\n", a);
+ x = a << 10;
+ printf("Signed left shift by 10: 0x%.2x\n\n", x);
+
+
+ printf("Unsigned Original value: 0x%.2x\n", b);
+ y = b << 10;
+ printf("Unsigned left shift by 10: 0x%.2x\n", y);
+
+ return 0;
+}
diff --git a/CSAPP/chap2/shifts.c b/CSAPP/chap2/shifts.c
new file mode 100644
index 0000000..836476d
--- /dev/null
+++ b/CSAPP/chap2/shifts.c
@@ -0,0 +1,39 @@
+/*
+ * Shift operations
+ *
+ * Right shifts might use either arithmetic shifts or logical shifts.
+ *
+ * This small piece of code should identify which one is used
+ */
+
+#include <stdio.h>
+
+int main(void)
+{
+ /* Using char so we care for a single byte only */
+ char x, a;
+ unsigned char y, b;
+ /* bin 10101001 */
+ a = b = 0xA9;
+
+ printf("Original signed variable: 0x%.2x\n", a);
+
+ /*
+ * Signed data variables are right shifted using Arithmetic shift. i.e.
+ * left bits are filled with a copy of the most significant bit of the
+ * original value
+ */
+ x = a >> 3;
+ printf("Signed right shift by 3: 0x%.2x\n\n", x);
+
+ printf("Original unsigned variable: 0x%.2x\n", b);
+ /*
+ * Unsigned data variables are right shifted logically. i.e. left bits
+ * are just filled with zeros
+ */
+ y = b >> 3;
+ printf("Unsigned right shift by 3: 0x%.2x\n", y);
+ return 0;
+}
+
+
diff --git a/CSAPP/chap2/show_types.c b/CSAPP/chap2/show_types.c
new file mode 100644
index 0000000..3f78b41
--- /dev/null
+++ b/CSAPP/chap2/show_types.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+int main(void) {
+ printf("Size of Char: %d\n", sizeof(char));
+ printf("Size of Short: %d\n", sizeof(short));
+ printf("Size of Int: %d\n", sizeof(int));
+ printf("Size of Long: %d\n", sizeof(long));
+ printf("Size of Float: %d\n", sizeof(float));
+ printf("Size of Double: %d\n", sizeof(double));
+ return 0;
+}
diff --git a/CSAPP/chap2/string.c b/CSAPP/chap2/string.c
new file mode 100644
index 0000000..9e783d4
--- /dev/null
+++ b/CSAPP/chap2/string.c
@@ -0,0 +1,11 @@
+#include <stdio.h>
+
+int main(void)
+{
+ char *a = "12345";
+ int i;
+
+ for (i=0; i < 6; i++)
+ printf("%.2x ", (unsigned int) a[i]);
+ return 0;
+}
diff --git a/CSAPP/chap3/main.c b/CSAPP/chap3/main.c
new file mode 100644
index 0000000..b4f7152
--- /dev/null
+++ b/CSAPP/chap3/main.c
@@ -0,0 +1,17 @@
+#include <stdio.h>
+
+void multstore(long, long, long*);
+
+int main(void)
+{
+ long d;
+ multstore(2, 3, &d);
+ printf("2 * 3 --> %ld\n", d);
+ return 0;
+}
+
+long mult2(long a, long b)
+{
+ long s = a * b;
+ return s;
+}
diff --git a/CSAPP/chap3/mstore.c b/CSAPP/chap3/mstore.c
new file mode 100644
index 0000000..8db0d29
--- /dev/null
+++ b/CSAPP/chap3/mstore.c
@@ -0,0 +1,7 @@
+long mult2(long, long);
+
+void multstore(long x, long y, long *dest)
+{
+ long t = mult2(x, y);
+ *dest = t;
+}
diff --git a/CSAPP/chap3/mstore.s b/CSAPP/chap3/mstore.s
new file mode 100644
index 0000000..78a6c6f
--- /dev/null
+++ b/CSAPP/chap3/mstore.s
@@ -0,0 +1,21 @@
+ .file "mstore.c"
+ .text
+ .globl multstore
+ .type multstore, @function
+multstore:
+.LFB0:
+ .cfi_startproc
+ pushq %rbx
+ .cfi_def_cfa_offset 16
+ .cfi_offset 3, -16
+ movq %rdx, %rbx
+ call mult2
+ movq %rax, (%rbx)
+ popq %rbx
+ .cfi_def_cfa_offset 8
+ ret
+ .cfi_endproc
+.LFE0:
+ .size multstore, .-multstore
+ .ident "GCC: (GNU) 10.1.1 20200507 (Red Hat 10.1.1-1)"
+ .section .note.GNU-stack,"",@progbits
diff --git a/CSAPP/chap3/mstore_intel.S b/CSAPP/chap3/mstore_intel.S
new file mode 100644
index 0000000..ca4fa2a
--- /dev/null
+++ b/CSAPP/chap3/mstore_intel.S
@@ -0,0 +1,22 @@
+ .file "mstore.c"
+ .intel_syntax noprefix
+ .text
+ .globl multstore
+ .type multstore, @function
+multstore:
+.LFB0:
+ .cfi_startproc
+ push rbx
+ .cfi_def_cfa_offset 16
+ .cfi_offset 3, -16
+ mov rbx, rdx
+ call mult2
+ mov QWORD PTR [rbx], rax
+ pop rbx
+ .cfi_def_cfa_offset 8
+ ret
+ .cfi_endproc
+.LFE0:
+ .size multstore, .-multstore
+ .ident "GCC: (GNU) 10.1.1 20200507 (Red Hat 10.1.1-1)"
+ .section .note.GNU-stack,"",@progbits
diff --git a/CSAPP/dsizes.c b/CSAPP/dsizes.c
new file mode 100644
index 0000000..7f57bc9
--- /dev/null
+++ b/CSAPP/dsizes.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+int main(void)
+{
+ int dpos32 = (-2147483648 > 0);
+ int hpos32 = (0x80000000 > 0);
+
+ printf("dpos32 %d - hpos32 %d\n", dpos32, hpos32);
+ printf("%ld\n", 1L << (sizeof(long) << 3) - 1);
+
+ return 0;
+}
diff --git a/CSAPP/leaq.c b/CSAPP/leaq.c
new file mode 100644
index 0000000..fe15ebc
--- /dev/null
+++ b/CSAPP/leaq.c
@@ -0,0 +1,4 @@
+long scale(long x, long y, long z) {
+ long t = x + 4*y + 12*z;
+ return t;
+}
diff --git a/CSAPP/leaq.s b/CSAPP/leaq.s
new file mode 100644
index 0000000..a97b842
--- /dev/null
+++ b/CSAPP/leaq.s
@@ -0,0 +1,35 @@
+ .file "leaq.c"
+ .text
+ .globl scale
+ .type scale, @function
+scale:
+.LFB0:
+ .cfi_startproc
+ pushq %rbp
+ .cfi_def_cfa_offset 16
+ .cfi_offset 6, -16
+ movq %rsp, %rbp
+ .cfi_def_cfa_register 6
+ movq %rdi, -24(%rbp)
+ movq %rsi, -32(%rbp)
+ movq %rdx, -40(%rbp)
+ movq -32(%rbp), %rax
+ leaq 0(,%rax,4), %rdx
+ movq -24(%rbp), %rax
+ leaq (%rdx,%rax), %rcx
+ movq -40(%rbp), %rdx
+ movq %rdx, %rax
+ addq %rax, %rax
+ addq %rdx, %rax
+ salq $2, %rax
+ addq %rcx, %rax
+ movq %rax, -8(%rbp)
+ movq -8(%rbp), %rax
+ popq %rbp
+ .cfi_def_cfa 7, 8
+ ret
+ .cfi_endproc
+.LFE0:
+ .size scale, .-scale
+ .ident "GCC: (GNU) 10.2.1 20200723 (Red Hat 10.2.1-1)"
+ .section .note.GNU-stack,"",@progbits
diff --git a/CSAPP/shift.c b/CSAPP/shift.c
new file mode 100644
index 0000000..3b088cf
--- /dev/null
+++ b/CSAPP/shift.c
@@ -0,0 +1,7 @@
+long shift(long x, long n) {
+ x <<= 4;
+ x >>= n;
+ return x;
+}
+
+
diff --git a/CSAPP/shift.s b/CSAPP/shift.s
new file mode 100644
index 0000000..c4a3bc7
--- /dev/null
+++ b/CSAPP/shift.s
@@ -0,0 +1,17 @@
+ .file "shift.c"
+ .text
+ .globl shift
+ .type shift, @function
+shift:
+.LFB0:
+ .cfi_startproc
+ movq %rdi, %rax
+ salq $4, %rax
+ movl %esi, %ecx
+ sarq %cl, %rax
+ ret
+ .cfi_endproc
+.LFE0:
+ .size shift, .-shift
+ .ident "GCC: (GNU) 5.3.1 20160406 (Red Hat 5.3.1-6)"
+ .section .note.GNU-stack,"",@progbits
diff --git a/CSAPP/tmul.c b/CSAPP/tmul.c
new file mode 100644
index 0000000..687221a
--- /dev/null
+++ b/CSAPP/tmul.c
@@ -0,0 +1,14 @@
+int tmul_ok_asm(long x, long y, long *dest )
+{
+ long r = x*y;
+ *dest = r;
+ return r > 0;
+}
+
+int umul_ok_asm(unsigned long x, unsigned long y, unsigned long *dest)
+{
+ unsigned long r = x*y;
+
+ *dest = r;
+ return r > 0;
+}
diff --git a/CSAPP/tmul.s b/CSAPP/tmul.s
new file mode 100644
index 0000000..52cee75
--- /dev/null
+++ b/CSAPP/tmul.s
@@ -0,0 +1,43 @@
+ .file "tmul.c"
+ .text
+ .globl tmul_ok_asm
+ .type tmul_ok_asm, @function
+tmul_ok_asm:
+.LFB0:
+ .cfi_startproc
+ imulq %rdi, %rsi
+ movq %rsi, (%rdx)
+# Code deleted
+# testq %rsi, %rsi
+# setg %al
+# Code inserted
+ setae %al
+# End of code inserted
+ movzbl %al, %eax
+ ret
+ .cfi_endproc
+.LFE0:
+ .size tmul_ok_asm, .-tmul_ok_asm
+ .globl umul_ok_asm
+ .type umul_ok_asm, @function
+umul_ok_asm:
+.LFB1:
+ .cfi_startproc
+ movq %rdx, %rcx #Save copy of dest
+ movq %rsi, %rax #copy y to %rax
+ mulq %rdi #unsigned multiplication
+ movq %rax, (%rcx) #copy result (low 64-bit) to dest
+ setae %al #Set low-order byt according to mulq
+# Code removed
+# imulq %rdi, %rsi
+# movq %rsi, (%rdx)
+# testq %rsi, %rsi
+# setne %al
+# End of code removed
+ movzbl %al, %eax
+ ret
+ .cfi_endproc
+.LFE1:
+ .size umul_ok_asm, .-umul_ok_asm
+ .ident "GCC: (GNU) 5.3.1 20160406 (Red Hat 5.3.1-6)"
+ .section .note.GNU-stack,"",@progbits
diff --git a/CSAPP/tmul_asm.c b/CSAPP/tmul_asm.c
new file mode 100644
index 0000000..e496618
--- /dev/null
+++ b/CSAPP/tmul_asm.c
@@ -0,0 +1,51 @@
+int tmul_bug(long x, long y, long *dest)
+{
+ long result = 0;
+ *dest = x*y;
+ asm("setae %al");
+ return result;
+}
+
+int tmul_ok(long x, long y, long *dest)
+{
+ int result = 0;
+ *dest = x*y;
+ asm("setae %%bl # Set low-order byte\n\t"
+ "movzbl %%bl, %[val] # Zero extend to be res"
+ : [val] "=r" (result) /* Output */
+ : /* No inputs */
+ : "%bl" /* Overwrites */
+ );
+ return result;
+}
+
+int tmul_ok2(long x, long y, long *dest)
+{
+ unsigned char result = 0;
+ *dest = x*y;
+
+ asm("setae %[b] # Set result"
+ : [b] "=r" (result) /* Output */
+ );
+ return (int)result;
+}
+
+int umult_ok(unsigned long x, unsigned long y, unsigned long *dest)
+{
+ /*
+ * GCC's asm choose the register to store asm operands
+ * according to the type/size of the variable
+ */
+ unsigned char result;
+
+ asm("movq %[x],%%rax #Get x\n\t"
+ "mulq %[y] # Unsigned long mult x*y\n\t"
+ "movq %%rax,%[p] #Store low-order 8bytes at dest\n\t"
+ "setae %[b] #Set result"
+ : [p] "=m" (*dest), [b] "=r" (result) /* Outputs */
+ : [x] "r" (x), [y] "r" (y) /* Inputs */
+ : "%rax", "%rdx" /* Overwrites */
+ );
+
+ return (int) result;
+}
diff --git a/CSAPP/tmul_asm.s b/CSAPP/tmul_asm.s
new file mode 100644
index 0000000..288a36a
--- /dev/null
+++ b/CSAPP/tmul_asm.s
@@ -0,0 +1,79 @@
+ .file "tmul_asm.c"
+ .text
+ .globl tmul_bug
+ .type tmul_bug, @function
+tmul_bug:
+.LFB0:
+ .cfi_startproc
+ imulq %rdi, %rsi
+ movq %rsi, (%rdx)
+#APP
+# 5 "tmul_asm.c" 1
+ setae %al
+# 0 "" 2
+#NO_APP
+ movl $0, %eax
+ ret
+ .cfi_endproc
+.LFE0:
+ .size tmul_bug, .-tmul_bug
+ .globl tmul_ok
+ .type tmul_ok, @function
+tmul_ok:
+.LFB1:
+ .cfi_startproc
+ pushq %rbx
+ .cfi_def_cfa_offset 16
+ .cfi_offset 3, -16
+ imulq %rdi, %rsi
+ movq %rsi, (%rdx)
+#APP
+# 13 "tmul_asm.c" 1
+ setae %bl # Set low-order byte
+ movzbl %bl, %eax # Zero extend to be res
+# 0 "" 2
+#NO_APP
+ popq %rbx
+ .cfi_def_cfa_offset 8
+ ret
+ .cfi_endproc
+.LFE1:
+ .size tmul_ok, .-tmul_ok
+ .globl tmul_ok2
+ .type tmul_ok2, @function
+tmul_ok2:
+.LFB2:
+ .cfi_startproc
+ imulq %rdi, %rsi
+ movq %rsi, (%rdx)
+#APP
+# 27 "tmul_asm.c" 1
+ setae %al # Set result
+# 0 "" 2
+#NO_APP
+ movzbl %al, %eax
+ ret
+ .cfi_endproc
+.LFE2:
+ .size tmul_ok2, .-tmul_ok2
+ .globl umult_ok
+ .type umult_ok, @function
+umult_ok:
+.LFB3:
+ .cfi_startproc
+ movq %rdx, %rcx
+#APP
+# 41 "tmul_asm.c" 1
+ movq %rdi,%rax #Get x
+ mulq %rsi # Unsigned long mult x*y
+ movq %rax,(%rcx) #Store low-order 8bytes at dest
+ setae %dil #Set result
+# 0 "" 2
+#NO_APP
+ movzbl %dil, %eax
+ ret
+ .cfi_endproc
+.LFE3:
+ .size umult_ok, .-umult_ok
+ .ident "GCC: (GNU) 5.3.1 20160406 (Red Hat 5.3.1-6)"
+ .section .note.GNU-stack,"",@progbits
diff --git a/CSAPP/xor.c b/CSAPP/xor.c
new file mode 100644
index 0000000..2cbf95a
--- /dev/null
+++ b/CSAPP/xor.c
@@ -0,0 +1,12 @@
+#include <stdio.h>
+
+int main(void) {
+
+ int x = 0xA14D6C50;
+
+ printf("Original: 0x%x\n", x);
+ printf("~x ^ 0xFF: 0x%x\n", ~x ^ 0xFF);
+ printf("x ^ ~0xFF: 0x%x\n", x ^ ~0xFF);
+
+ return 0;
+}