This assignment will run through the second program everyone writes, prompting for the user’s name and then greeting them by name. When you run the final program, it should work like this:
What is your name: Andy
Hello, Andy, nice to meet you!
(Where Andy
is what I typed in; the rest was printed by the program.)
In order to do this, you will need to add another syscall to your repertoire:
syscall code 0 is SYS_READ
, which reads from a stream up to a newline or a
maximum length (whichever comes first) and stores the resulting bytes into a
buffer.
rax = 0
,SYS_READ
rdi =
the stream to read from. Standard input (stdin
) is stream 0.rsi =
the address of the beginning of the buffer to save the read-in bytes intordx =
the maximum number of bytes to read in (i.e. the length of the buffer). Reading will stop after this many bytes, or after a newline character, whichever comes first.
Note that the characters stored in the buffer are not NUL-terminated; instead,
the syscall will “return” the number of bytes read in, including the newline,
by storing it into rax
. Because you need to use rax
for other things, you’ll
want to copy this value into one of the registers that is not used by
syscalls. This length will include the newline that ended the input, so you’ll
probably want to subtract 1 to prevent that from appearing in the output.
There’s no way to “concatenate” strings in assembly, so to print the final
message you will need three syscalls: one to print Hello,
, a second to
print the contents of the buffer, and a third to print , nice to meet you!
.
When you print the contents of the buffer, the length you should use is not
the BUFFER_LEN
, because it’s unlikely that the user typed in a name that
filled the entire buffer; instead, use the length returned by the SYS_READ
syscall (which you should have saved into a different register).
Like all syscall-style programs, this one must end with a SYS_EXIT = 60
syscall, to correctly exit the program.
Here’s a skeleton that you can use to start:
;;;
;;; greet.s
;;; Prompt the user for their name, then greet them by name.
;;; At runtime, the program should work like this:
;;;
;;; What is your name: Andy <--- Andy is user input
;;; Hello, Andy, nice to meet you!
;;;
section .data
prompt: db "What is your name: "
PROMPT_LEN: equ $ - prompt
greet1: db "Hello, "
GREET1_LEN: equ $ - greet1
greet2: db ", nice to meet you!", 10
GREET2_LEN: equ $ - greet2
; For user input, we need an "empty" buffer of space to store the input into.
; This reserves 256 bytes of space, filled with 0s, for that purpose.
BUFFER_LEN: equ 256
buffer: times BUFFER_LEN db 0
section .text
global _start
_start:
; Your code here
Save your submission in cs241/group1/
. You only need one submission per group,
if you want.
You can find a copy of this skeleton on the server in
/usr/local/class/src/cs241_assign1.s
. To copy it into your current directory,
run the command
cp /usr/local/class/src/cs241_assign1.s .
(NOTE: The .
at the end of the command is part of the command!)