Homework 0

This commit is contained in:
Carlos Galindo 2020-01-15 22:17:50 +01:00
commit 49bca7f856
Signed by: kauron
GPG Key ID: 83E68706DEE119A3
2 changed files with 78 additions and 0 deletions

5
HW0/Makefile Normal file
View File

@ -0,0 +1,5 @@
all:
gcc -m32 -o read-write read-write.s
clean:
rm -f read-write

73
HW0/read-write.s Normal file
View File

@ -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: