Question: 1 . ( 3 0 points ) Using the echo _ nsl . s program below, write an assembly program that inputs a single character

1.(30 points) Using the echo_nsl.s program below, write an assembly program that inputs a single
character from stdin and then prints the character before it to stdout. For example, if you enter a D,
your program will output a C. In order for your character to be passed to your program, you will
have to press the enter key to flush the input buffer. Your program will output only one character.
Name your program pred_nsl.s.
2.(60 points) Read two decimal digits from stdin and interpret that input as an integer between 0 an
99, inclusive. Compute 27x +17, where x is the provided integer. Print the input value, the result in
decimal and hexadecimal like this:
x =19 : f(x)=530 or 0x212
Spacing matters. You can get the integer value of an ASCII digit by subtracting 0 from it. The
appendix has an example of using
Name your program fx.s.
3.10 points Write prompts for the two programs above and submit them to the LLM of your choice. Save
the resulting code (not any explanation that came with it) into pred_prom.s and fx_prom.s. Include
the prompt as a comment at the top of the source file. Also include a critique of the geneated code in
the introductory comments as well.
Appendix
The following program reads a single character and outputs the character following it in the ASCII encoding.
It uses Linux system calls to perform I?O with sys_read and sys_write. It uses the _start label and is in
conflict with Cs stdlib library. The -nostdlib directive must be added for linking to succeed.
# echo_nsl.s
# Read one character from stdio, add one to the ASCII value, and write
# it on stdout.
# gcc -nostdlib -no-pie echo_nsl.s -o echo_nsl
# this line prevents a loader warning
.section .note.GNU-stack,"",@progbits
.section .data
.comm buffer, 1 # Allocate 1 byte for the input character
.section .text
.global _start
# This program does not use stdlib. The linker recognizes _start
# as the beginning of execution.
_start:
# Read a single character from stdin
movq $0,%rax # syscall number for sys_read (0)
movq $0,%rdi # file descriptor for stdin (0)
movq $buffer, %rsi # pointer to the buffer
movq $1,%rdx # number of bytes to read
syscall
# Add 1 to the ASCII value of the character
movb buffer, %al # Load the byte from buffer into AL
addb $1,%al # Increment the ASCII value by 1
movb %al, buffer # Store the result back to buffer
# Write the modified character to stdout
1
movq $1,%rax movq $1,%rdi movq $buffer, %rsi movq $1,%rdx syscall
# Exit the program
movq $60,%rax movq $0,%rdi syscall
# syscall number for sys_write (1)
# file descriptor for stdout (1)
# pointer to the buffer
# number of bytes to write
# syscall number for sys_exit (60)
# status =0
The following program does some computation and then uses printf to output the values. It requires stdlib.
Since stdlib defines _start, the entry point for this program will be main.
# printf.s
# This program adds 48 and 29 and then outputs the sum in hex. It
# uses stdlib.
# gcc -no-pie echo.s -o echo
# prevents missing .note.GNU-stack error
.section .note.GNU-stack,"",@progbits
#.extern putchar
.section .data
num1: .word 48
num2: .word 29
# The h in the format specifier is a length parameter.
# Printf expects a unsigned short int.
format: .string "result =%04hx
\0"
.section .text
.global main
main:
# Load the values into registers
movl num1,%eax
movl num2,%ebx
# Add the values
addl %ebx, %eax
# save the callee of main, call to printf will wipe it out
push %rbp
# Call printf to print the result:
# printf("%04hx
", sum)
# loads effective address of format string
# into first argument register
lea format, %rdi
# move value we want to print into the
# second argument register
movl %eax, %esi
call printf
# Restore the callee that was overwritten by
# call to printf
pop %rbp
ret
Heres a makefile to help you out. The gcc lines are preceded by tabs, not spaces.
%_nsl: %_nsl.s
gcc -nostdlib -no-pie $<-o %@
%: %.s
gcc -no-pie $<-o $@

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!