I am using Intel i386 assembly language programming Write a C-callable assembler
ID: 3808160 • Letter: I
Question
I am using Intel i386 assembly language programming
Write a C-callable assembler version of the library strcpy function called mystrcpy (to avoid conflicts with the library version) that copies the contents of one string to a user provided array and returns a pointer to the provided array. The function prototype is:
char *mystrcpy(char *s, char *ct);
Write your code in a source file named strcpy.s. The provided C driver (strcpyc.c) takes user entered input for the source string and checks both the pointer returned and the effect of the copy. Choose test cases that exercise different possibilities in the logic of your code, e.g. a null string. What would happen if you choose a string longer than the destination array in the Cdriver?
Explanation / Answer
#include static inline int mystrcmp(const char *cs,const char *ct) { register int __res; __asm__ __volatile__( "cld " "1: " "lodsb " "scasb " "jne 2f " "testb %%al,%%al " "jne 1b " "xorl %%eax,%%eax " "jmp 3f " "2: " "sbbl %%eax,%%eax " "orb $1,%%al " "3: " :"=a" (__res):"S" (cs),"D" (ct):"si","di"); return __res; } static inline char *mystrcpy(char *dest, const char *src) { int d0, d1, d2; __asm__ __volatile__( "1: " " lodsb " "stosb " "testb %%al,%%al " "jne 1b" : "=&S" (d0), "=&D" (d1), "=&a" (d2) : "0" (src),"1" (dest) : "memory"); return dest; } int main(void) { int i; char buf[] = "i am really cool too "; char str[] = "i am so cool........ "; char *ret = mystrcpy(buf, str); for (i=0;iRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.