Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with an ARM assembly function. I am trying to emulate the function of the following C function in ARM assembly: The function is

Need help with an ARM assembly function. I am trying to emulate the function of the following C function in ARM assembly:

image text in transcribed

The function is supposed to be called in C and used in the following C code as "strncpy":

image text in transcribed

Shown below is my current ARM assembly code, but it doesn't work properly. It compiles but does not output any values. I'm not sure what I have done wrong, maybe it's the syntax I'm not understanding?

AREA |.text|, CODE, READONLY

EXPORT strncpy

strncpy

; r0 = char* s1, r1 = char* s2, r2 = char n MOV r3, r0; Copy s1 to r3 (char* s = s1)

loop1 ; While loop CMP r1, #0; End loop if *s1 != '/0' BEQ loop2; CMP r2, #1; End loop if n > 0 BLT loop2; MOV r3, r1; Saves char at s2 pos to char at s pos (*s = *s2) ADD r3, r3, #1; Increments s pos (*s++) ADD r1, r3, #1; Increments s2 pos (*s2++) SUB r2, r2, #1; Decrements char n (--n) B loop1; Jumps to top of loop1 loop2 ; 2nd while loop CMP r2, #1; End loop if n > 0 BLT endloop; ;AND r3, r3, #0; Clears r3 register MOV r3, #0; Makes r3 register '0' (*s = '\0') ADD r3, r3, #1; Increments s pos (*s++) B loop2; Jumps to top of loop2 endloop MOV r0, r3; Copy contents of r3 to s1 for return (return s1) BX r14; END

char *strncpy(char *51, const char *52, char n) { char* s = s1; while(n>0 && *52 != '\0') { *S++ = *$2++; --n; } while(n > 0) { *5++ = 'le'; --n; } return 51; } extern char *strncpy(char *, const char *, char); ]/*--- Main Program ]int main (void) { char strl[20]; SER_Init(); strncpy (stri,"123456", 5); stri[5] = '\0'; printf("%s ", strl);|| strncpy (stri,"123456",6); stri[6] = '\0'; printf("%s ", strl); strncpy(stri,"123456", 1); stri[1]='\0'; printf("%s ", strl); strncpy (stri,"123456",8); printf("%s ", strl); strncpy (stri,"",1); printf("%s ", strl); return 0; }

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

More Books

Students also viewed these Databases questions