CMSC313, Computer Organization & Assembly Language Programming, Spring 2013
Project 7: String Functions in Assembly
Due: Tuesday April 16, 2013, 11:59pm
Objective
The objective of this programming exercise is to practice writing
assembly language programs that use the C function call conventions.
Assignment
Your assignment is to implement two functions in assembly language program that
use the C function call conventions. If you missed class when C function
call conventions were discussed, you should read the write up on C Function Call Conventions before asking any
questions. (Yes, it's complicated.)
The two functions that you must implement in assembly language have the
following prototypes:
void strrev(char *str) ;
int strrepl(char *str, int c, int (* isinsubset) (int c) ) ;
The strrev() function reverses the characters in the memory
location addressed by the pointer str. For example, if
strrev() is given the string "Hello World", then
the string is changed to "dlroW olleH" after calling
strrev(). In the call strrev(ptr), the pointer
ptr must point to a memory location that can be modified.
The call strrev("abcd") will result in a segmentation fault
because "abcd" is a constant string.
The strrepl() function replaces each occurrence of a subset of
characters in string addressed by str with the character
c. The subset is specified by the third parameter, a function
pointer, isinsubset(). The return value of strrepl()
is the number of characters that were replaced.
For example, the following call to strrepl()
str1 = strdup("ABC 123 779 Hello World") ;
r = strrepl(str1, '#', &isdigit) ;
will change the string addressed by str1 to "ABC ### ###
Hello World" and will return the value 6. Here isdigit()
is a standard C Library function that returns non-zero if given a digit
character ('0' – '9') and returns zero otherwise.
Similarly, the call
str3 = strdup("ABC 123 779 Hello World") ;
r = strrepl(str3, '_', &isvowel) ;
will changed the string addressed by str3 to
"_BC 123 779 H_ll_ W_rld", assuming that isvowel()
is a function that identifies vowels.
The function pointers passed as the third parameter to
strrepl() must have the function signature:
int function_name (int c) ;
Even though the intention is to pass a character to these functions, the
parameter is nevertheless an int. This is because the
end-of-file character (EOF) is often represented as -1, and C has
traditionally allowed EOF to be passed to functions such as
isdigit. (See man pages for ctype.)
A test program, main7.c, has been
provided for you along with its output: main7.txt. As usual, testing is your
responsibility and you should exercise your functions with your own
test programs.
Implementation Notes:
- Place your assembly language implementation of the two
functions in separate files: strrev.asm and
strrepl.asm.
- Please name your test program test7.c.
- You must make labels strrev and strrepl
global in their respective .asm files (and you must
have labels called strrev and strrepl).
- Examples of assembly language programs using C function pointers
are available here: Lecture 18.
- Remember that C strings are null-terminated and that the only way
to determine the length of the string is to find the null character.
- When a char is passed to an int parameter,
it is extended to 4 bytes in little endian. (Actually, whenever
a char is passed, 4 bytes are pushed on the stack.)
- In our previous assembly language programs, we used the
CALL instruction only with immediate operands:
call subroutine1
However, CALL also allows r/m32 operands, so the address of
the target subroutine can also be in a 32-bit register or a
memory location.
- Both strrev() and strrepl() should do nothing
when called with a NULL pointer. In particular, strrev()
and strrepl() should not crash in these situations.
Additionally, strrepl() should return 0 for the number
of characters replaced.
- Review the C Function Call Conventions regarding which registers
must be saved and restored and which may be altered by a function.
- Remember that when you call a C function from your
assembly code, the C function is allowed to change some of the
registers. So, choose your registers carefully.
Turning in your program
Use the UNIX submit command on the GL system to turn in your
project. When you are done, use the script Unix command to
record yourself running your program. You do not have record
yourself using gdb for this project.
You should submit 4 files: strrev.asm, strrepl.asm,
test7.c and typescript.
The UNIX command to submit should look like:
submit cs313 proj7 strrev.asm strrepl.asm test7.c typescript
Last Modified:
22 Jul 2024 11:28:18 EDT
by
Richard Chang
to Spring 2013 CMSC 313 Homepage