commit f39b67e87d1667212cd58c88fcaa7f6795975b69 Author: Zev Averbach Date: Wed Nov 8 14:27:21 2023 +0000 first diff --git a/add_and_subtract b/add_and_subtract new file mode 100755 index 0000000..4abb96c Binary files /dev/null and b/add_and_subtract differ diff --git a/add_and_subtract.hla b/add_and_subtract.hla new file mode 100644 index 0000000..50d8036 --- /dev/null +++ b/add_and_subtract.hla @@ -0,0 +1,26 @@ +program AddAndSub; + +#include("stdlib.hhf") + +static + eight_bit_integer: int8 := 7; + +begin AddAndSub; + + stdout.put + ( + nl, + "Initialized values: eight_bit_integer=", + eight_bit_integer, + nl + ); + + mov( 0, dh ); // `al` is an eight-bit register + sub( eight_bit_integer, dh ); // `al` minus `eight_bit_integer` + mov( dh, eight_bit_integer ); // this should be -7 + + stdout.put(nl, "Now eight_bit_integer = ", eight_bit_integer, nl); + // add( 135, eight_bit_integer); + // stdout.put(nl, "Now eight_bit_integer = ", eight_bit_integer, nl); + +end AddAndSub; diff --git a/add_and_subtract.o b/add_and_subtract.o new file mode 100644 index 0000000..877e718 Binary files /dev/null and b/add_and_subtract.o differ diff --git a/dec b/dec new file mode 100755 index 0000000..5028595 Binary files /dev/null and b/dec differ diff --git a/dec.hla b/dec.hla new file mode 100644 index 0000000..6a400b0 --- /dev/null +++ b/dec.hla @@ -0,0 +1,12 @@ +program ConvertToDecimal; +#include("stdlib.hhf") +static + value: int32; + +begin ConvertToDecimal; + + stdout.put("Input a hex value: "); + stdin.get(eax); + mov(eax, value); + stdout.put("The value $", eax, " converted to decimal is ", value, nl); +end ConvertToDecimal; diff --git a/dec.o b/dec.o new file mode 100644 index 0000000..09f9074 Binary files /dev/null and b/dec.o differ diff --git a/fasm_with_tsoding/.server.asm.swp b/fasm_with_tsoding/.server.asm.swp new file mode 100644 index 0000000..151eb6b Binary files /dev/null and b/fasm_with_tsoding/.server.asm.swp differ diff --git a/fasm_with_tsoding/.steps.md.swp b/fasm_with_tsoding/.steps.md.swp new file mode 100644 index 0000000..7a95086 Binary files /dev/null and b/fasm_with_tsoding/.steps.md.swp differ diff --git a/fasm_with_tsoding/hello b/fasm_with_tsoding/hello new file mode 100755 index 0000000..32e7fb4 Binary files /dev/null and b/fasm_with_tsoding/hello differ diff --git a/fasm_with_tsoding/hello.asm b/fasm_with_tsoding/hello.asm new file mode 100644 index 0000000..3f6e686 --- /dev/null +++ b/fasm_with_tsoding/hello.asm @@ -0,0 +1,34 @@ +format ELF64 executable + +STDOUT = 1 +HAPPY_EXIT_CODE = 1 +SYSCALL_EXIT = 60 +SYSCALL_WRITE = 1 + +macro print fd, buf, len +{ + mov rax, SYSCALL_WRITE ; 1 -> write, which takes three arguments (below) + mov rdi, fd + mov rsi, buf + mov rdx, len + syscall +} + +macro exit code +{ + mov rax, SYSCALL_EXIT + mov rdi, HAPPY_EXIT_CODE + syscall +} + +segment readable executable +entry main +main: + print STDOUT, msg, msg_len + exit 1 + + +segment readable writeable +msg db "Hello, World!", 10 +msg_len db $ - msg + diff --git a/fasm_with_tsoding/server b/fasm_with_tsoding/server new file mode 100755 index 0000000..0782fec Binary files /dev/null and b/fasm_with_tsoding/server differ diff --git a/fasm_with_tsoding/server.asm b/fasm_with_tsoding/server.asm new file mode 100644 index 0000000..fa5610b --- /dev/null +++ b/fasm_with_tsoding/server.asm @@ -0,0 +1,49 @@ +format ELF64 executable + +STDOUT = 1 +HAPPY_EXIT_CODE = 0 +SYSCALL_EXIT = 60 +SYSCALL_WRITE = 1 +SYSCALL_SOCKET = 41 +AF_INET = 2 +SOCK_STREAM = 1 + +macro print buf, len +{ + mov rax, SYSCALL_WRITE ; 1 -> write, which takes three arguments (below) + mov rdi, STDOUT + mov rsi, buf + mov rdx, len + syscall +} + +macro exit code +{ + mov rax, SYSCALL_EXIT + mov rdi, code + syscall +} + +macro socket domain, type, protocol +{ + mov rax, SYSCALL_SOCKET + mov rdi, domain + mov rsi, type + mov rdx, protocol + syscall +} + +segment readable executable +entry main +main: + print msg, msg_len + socket AF_INET, SOCK_STREAM, 0 + mov dword [socket_fd], eax + print [socket_fd], 32 + exit HAPPY_EXIT_CODE + + +segment readable writeable +msg db "Starting web server!", 10 +msg_len db $ - msg +socket_fd dd 0 diff --git a/fasm_with_tsoding/steps.md b/fasm_with_tsoding/steps.md new file mode 100644 index 0000000..841d5c7 --- /dev/null +++ b/fasm_with_tsoding/steps.md @@ -0,0 +1,10 @@ +1) install fasm ([link](https://flatassembler.net/download.php)) +```bash +sudo apt install build-essential manpages-dev software-properties-common +sudo add-apt-repository ppa:ubuntu-toolchain-r/test +sudo apt update && sudo apt install gcc-11 g++-11 +``` +1) `sudo apt install libx11-dev` +1) install gdb +1) install [gf](git clone https://github.com/nakst/gf.git) + diff --git a/hello b/hello new file mode 100755 index 0000000..17cdc95 Binary files /dev/null and b/hello differ diff --git a/hello.hla b/hello.hla new file mode 100644 index 0000000..34a99b2 --- /dev/null +++ b/hello.hla @@ -0,0 +1,8 @@ +program helloWorld; +#include( "stdlib.hhf" ); + +begin helloWorld; + + stdout.put( "Hello, World of Assembly Language" nl ); + +end helloWorld; diff --git a/hello.o b/hello.o new file mode 100644 index 0000000..3b47637 Binary files /dev/null and b/hello.o differ diff --git a/hex b/hex new file mode 100755 index 0000000..ea44f30 Binary files /dev/null and b/hex differ diff --git a/hex.hla b/hex.hla new file mode 100644 index 0000000..e9942b9 --- /dev/null +++ b/hex.hla @@ -0,0 +1,12 @@ +program ConvertToHex; +#include("stdlib.hhf") +static + value: int32; + +begin ConvertToHex; + + stdout.put("Input a decimal value: "); + stdin.get(value); + mov(value, eax); + stdout.put("The value ", value, " converted to hex is $", eax, nl); +end ConvertToHex; diff --git a/hex.o b/hex.o new file mode 100644 index 0000000..b76a829 Binary files /dev/null and b/hex.o differ diff --git a/logical_ops b/logical_ops new file mode 100755 index 0000000..a80bc2e Binary files /dev/null and b/logical_ops differ diff --git a/logical_ops.hla b/logical_ops.hla new file mode 100644 index 0000000..8c59bbe --- /dev/null +++ b/logical_ops.hla @@ -0,0 +1,13 @@ +program LogicalOp; +#include("stdlib.hhf") +begin LogicalOp; + stdout.put( "Input left operand: "); // 5 -> 0000_0000_0000_0101 + stdin.get( eax ); + stdout.put( "Input right operand: "); + stdin.get( ebx ); // 15 -> 0000_0000_0000_1111 + + stdout.put( "$", eax, " xor $", ebx, " = $"); + // mov (eax, ecx ); + xor (eax, ebx ); + stdout.put( ebx, nl); // 10 +end LogicalOp; diff --git a/logical_ops.o b/logical_ops.o new file mode 100644 index 0000000..b44992d Binary files /dev/null and b/logical_ops.o differ diff --git a/sign_extension.hla b/sign_extension.hla new file mode 100644 index 0000000..0f6ffd6 --- /dev/null +++ b/sign_extension.hla @@ -0,0 +1,20 @@ +program signExtension; +#include( "stdlib.hhf" ) + +static + i8: int8; + i16: int16; + i32: int32; + +begin signExtension; + + stdout.put("Enter a small negative number: "); + stdin.get(i8); + stdout.put(nl, "Sign extension using CBW and CWDE:", nl, nl); + + mov(i8, al); + stdout.put("You entered ", i8, " ($", al, ")", nl); + + cbw(); // ??? must be sign extending the value in `al` to `ax` + mov(ax, i16); + stdout.put("16-bit sign extension: ", i16, " ($", ax, ")", nl); diff --git a/twos_complement b/twos_complement new file mode 100755 index 0000000..3809e42 Binary files /dev/null and b/twos_complement differ diff --git a/twos_complement.hla b/twos_complement.hla new file mode 100644 index 0000000..c005cbf --- /dev/null +++ b/twos_complement.hla @@ -0,0 +1,25 @@ +program twosComplement; +#include("stdlib.hhf") + +static + pos_value: int8; + neg_value: int8; + +begin twosComplement; + + stdout.put( "Enter an integer between 0 and 127: " ); + stdin.get( pos_value ); + stdout.put( nl, "Value in hex: $" ); + stdout.puth8( pos_value ); + + mov( pos_value, al ); + // not ( al ); + // stdout.put( nl, "Invert all the bits: $", al, nl); + // add( 1, al ); + neg( al ); + // stdout.put( "Add one: $", al, nl ); + mov( al, neg_value ); + stdout.put( nl, "Result in decimal: ", neg_value, nl); + +end twosComplement; + diff --git a/twos_complement.o b/twos_complement.o new file mode 100644 index 0000000..3d6de71 Binary files /dev/null and b/twos_complement.o differ diff --git a/variables b/variables new file mode 100755 index 0000000..d335ca5 Binary files /dev/null and b/variables differ diff --git a/variables.hla b/variables.hla new file mode 100644 index 0000000..31a71bc --- /dev/null +++ b/variables.hla @@ -0,0 +1,15 @@ +program Vars; +#include( "stdlib.hhf" ) + +static + MyNum: int8 := 42; + NumNotInitialized: int8; + +begin Vars; + + stdout.put( "MyNum = ", MyNum, nl); + stdout.put( "Enter an integer value: " ); + stdin.get(NumNotInitialized); + stdout.put( "You entered '", NumNotInitialized, "'", nl ); + +end Vars; diff --git a/variables.o b/variables.o new file mode 100644 index 0000000..fcb6cc6 Binary files /dev/null and b/variables.o differ diff --git a/zero_extension b/zero_extension new file mode 100755 index 0000000..c5f0ec6 Binary files /dev/null and b/zero_extension differ diff --git a/zero_extension.hla b/zero_extension.hla new file mode 100644 index 0000000..731f4eb --- /dev/null +++ b/zero_extension.hla @@ -0,0 +1,14 @@ +program zero_extension; +#include("stdlib.hhf") + +static + magic_num: int8 := 42; + magic_num_16: int16; + +begin zero_extension; + mov(magic_num, al); + mov(0, ah); + mov(ax, magic_num_16); + stdout.put("magic_num_16 = ", magic_num_16); // should be 42 +end zero_extension; + diff --git a/zero_extension.o b/zero_extension.o new file mode 100644 index 0000000..1183e2f Binary files /dev/null and b/zero_extension.o differ