From 49bca7f856d25370485f36ac51d006cd08161cf7 Mon Sep 17 00:00:00 2001 From: Carlos Galindo Date: Wed, 15 Jan 2020 22:17:50 +0100 Subject: [PATCH] Homework 0 --- HW0/Makefile | 5 ++++ HW0/read-write.s | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 HW0/Makefile create mode 100644 HW0/read-write.s diff --git a/HW0/Makefile b/HW0/Makefile new file mode 100644 index 0000000..20ec076 --- /dev/null +++ b/HW0/Makefile @@ -0,0 +1,5 @@ +all: + gcc -m32 -o read-write read-write.s + +clean: + rm -f read-write diff --git a/HW0/read-write.s b/HW0/read-write.s new file mode 100644 index 0000000..988cc0b --- /dev/null +++ b/HW0/read-write.s @@ -0,0 +1,73 @@ +.section .data +inp_buf: .byte 0 + +.section .text + +.globl main +# /*; System call to read from standard input into buffer +# ; eax= code of syscall (read = 3) +# ; Arguments: ebx= file descriptor (0 is stdin) +# ; ecx= pointer to buffer +# ; edx= maximum number of bytes to read +# ; Return value: eax= number of bytes read*/ +main: + # Read into buffer + mov $3, %eax + mov $0, %ebx + mov $inp_buf, %ecx + mov $1, %edx + int $0x80 + + # Transform from ascii to int + sub $0x30, (inp_buf) + + mov (inp_buf), %esi + +loop: + # Read new value + mov $3, %eax + mov $0, %ebx + mov $inp_buf, %ecx + mov $1, %edx + int $0x80 + # if non number, jump to end + cmp $0xA, (inp_buf) + je end + # Substract 0x30 + sub $0x30, (inp_buf) + # Multiply esi by 10 + imul $10, %esi + # Add value to esi + add (inp_buf), %esi + jmp loop +end:# Print result + + add $1, %esi + mov %esi, %eax + + xorl %esi, %esi + +loop2: + movl $0, %edx + movl $10, %ebx + divl %ebx + addl $48, %edx + push %edx + incl %esi + cmpl $0, %eax + jz next + jmp loop2 + +next: + cmpl $0, %esi + jz exit + decl %esi + movl $4, %eax + movl %esp, %ecx + movl $1, %ebx + movl $1, %edx + int $0x80 + addl $4, %esp + jmp next + +exit: