Skip to content

Instantly share code, notes, and snippets.

@EdoardoVignati
Created October 25, 2018 07:24
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EdoardoVignati/aa91ec281cac37d26f058bbd8812d846 to your computer and use it in GitHub Desktop.
Save EdoardoVignati/aa91ec281cac37d26f058bbd8812d846 to your computer and use it in GitHub Desktop.
Read from stdin and print input - AT&T assembly
#################
# How to run #
#################
# $ as --gstabs read.s -o read.o
# $ ld read.o -o read
# $ ./read
################
.text
.globl _start
MAX_CHAR=30
_start:
## Start message ##
movl $4, %eax
movl $1, %ebx
movl $msg, %ecx
movl $len, %edx
int $0x80
## READ ##
movl $3, %eax #sys_read (number 3)
movl $0, %ebx #stdin (number 0)
movl %esp, %ecx #starting point
movl $MAX_CHAR, %edx #max input
int $0x80 #call
## Need the cycle to count input length ##
movl $1, %ecx #counter
end_input:
xor %ebx, %ebx
movb (%esp), %bl
add $1, %esp #get next char to compare
add $1, %ecx #counter+=1
cmp $0xa, %ebx #compare with "\n"
jne end_input #if not, continue
## WRITE ##
sub %ecx, %esp #start from the first input char
movl $4, %eax #sys_write (number 4)
movl $1, %ebx #stdout (number 1)
movl %ecx, %edx #start pointer
movl %esp, %ecx #length
int $0x80 #call
## EXIT ##
movl $1, %eax
int $0x80
.data
msg: .ascii "Insert an input:\n"
len =.-msg
@clonq-ht
Copy link

clonq-ht commented May 5, 2021

xor usding for what ?

@EdoardoVignati
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment