Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. Write a C callable assembler function that counts the number of characters in a string. The function should work for any string and character.

1. Write a C callable assembler function that counts the number of characters in a string. The function should work for any string and character. The address of the string and character to be counted are passed as arguments according to the C function prototype: int count (char *string, char c); Since there are arguments, your assembly function should use a stack frame. It should return the count to the calling C program in %eax. Use 32-bit quantities for all data. Even though chars have only 8 bits, they are stored in memory (and on the stack) as 32 bits in little endian format. If the 32-bit value is moved from memory to a register, the char value is available in the 8 least significant bits of the register. You are given a C calling program that calls count to count the number of a user-entered character in a user-entered string and prints the result. The C code driver is in countc.c. Put your assembly code in count.s. Then build it using the provided makefile by invoking make A=count. In count.script, provide a script showing a run with a breakpoint set using gdb where the count is incremented, showing the count (in a register) each time the breakpoint is hit, just before the increment is made. In the script, show how you determined where to set the breakpoint, i.e., how you determined where the increment instruction is located in memory. Files

count.c

extern int count(char *string, char c);

int main(void) { char s[100]; char c; printf("Enter a string of characters: "); scanf("%s", s); printf("Enter a character to count: "); scanf("%c", &c); printf(" The number of %c's in the string %s is %d ", c, s, count(s, c)); return 0; }

makefile

# makefile: makefile for assembly-language examples # # Assumes a matched pair of sources, e.g., myprog.s and myprogc.c, # an assembler source for a function and its C driver.

# Usage: For an assembler source, say myprog.s, with a corresponding # C driver myprogc.c, set A to the basename in the make command line: # "make A=myprog myprog.lnx" or just "make A=myprog" # (since $(A).lnx is the first make target) # Then myprog.lnx is ready for downloading via "mtip -f myprog.lnx" # (followed by ~r and ~d inside the mtip program)

# also "make clean" to clean up non-source files in a directory

# Here the assembler program has the primary source name, # the C program is just a driver of almost the same basename: C = $(A)c

# system directories needed for compilers, libraries, header files-- # assumes the environment variables SAPC_TOOLS, SAPC_GNUBIN, and SAPC_SRC # are set up, usually by the ulab module

PC_LIB = $(SAPC_TOOLS)/lib PC_INC = $(SAPC_TOOLS)/include

CC = $(SAPC_GNUBIN)/i386-gcc CFLAGS = -g -Wall -Wno-implicit -Wshadow -I$(PC_INC) AS = $(SAPC_GNUBIN)/i386-as LD = $(SAPC_GNUBIN)/i386-ld NM = $(SAPC_GNUBIN)/i386-nm

# File suffixes: # .c C source (often useful both for UNIX host and SAPC) # .s assembly language source # .opc relocatable machine code, initialized data, etc., for SA PC # .lnx executable image (bits as in memory), for SA PC (Linux a.out format) # .syms text file of .lnx's symbols and their values (the "symbol table") # Symbol file "syms"--for most recently built executable in directory

# PC executable--tell ld to start code at 0x1000e0, load special startup # module, special PC C libraries-- # Code has 0x20 byte header, so use "go 100100" (hopefully easier to # remember than 100020)

$(A).lnx: $(A).opc $(C).opc $(LD) -N -Ttext 1000e0 -o $(A).lnx \ $(PC_LIB)/startup0.opc $(PC_LIB)/startup.opc \ $(C).opc $(A).opc $(PC_LIB)/libc.a rm -f syms;$(NM) -n $(A).lnx>$(A).syms;ln -s $(A).syms syms

$(A).opc: $(A).s $(AS) -o $(A).opc $(A).s

# tell gcc to use $(PC_INC) for #include <...> headers-- $(C).opc: $(C).c $(CC) $(CFLAGS) -c -o $(C).opc $(C).c

clean: rm -f *.o *.opc *.syms *.lnx core syms

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

10th Edition

0137916787, 978-0137916788

More Books

Students also viewed these Databases questions

Question

=+j Explain IHRMs role in global HR research.

Answered: 1 week ago

Question

=+j Describe an effective crisis management program.

Answered: 1 week ago