Description
For this assignment, you are to write the strcpy function in 8051 assembly and test them in EdSim51. The purpose of this function is to calculate the length of a certain string and copy the string in another location in the memory.
The pseudocode is shown on the left, and the template for the assembly is on the right:
// C-like version pseudocode
char string1[ ] = “your test string”; // null terminated char string1Len = 16; char string2[ ] = “”; char string2Len = 0; // Append the length after the null character // Try five strings, different lengths etc char t = 0; // Global (in a register) tracking test cases char strcpy ( char *s, char * sd ) { char len = 0; while(s[len] != ‘\0’) { sd[len]=s[len]; // Another variable in another memory location ++len; } return len; }
void testString( char *s) { char* sd; char len = strcpy( s, sd ); char expectedLen = s[len+1]; char* expectedstr = sd; if ( len != expectedLen ) goto Error; if ( strcmp( expectedstr, s ) != 0 ) goto Error; t++; } void main() { testString(string1); testString(string2); …. // More test cases SUCCESS: goto SUCCESS; ERROR: goto ERROR;
|
;; Template for hw2.asm
;; Put status of your code here: does it work? has bugs? etc ;; If it doesn’t work, what do you think is wrong with it? ORG 0H STRING1: DB “your test string” ;; string data DB 0 ;; Null termination STRING1LEN: 16 ;; String length follows the null STRING2: DB ……. STRCPY: ;; The caller passes source string pointer in DPTR (in ROM). ;; The caller passes destination string pointer in R0 (in RAM). ;; The caller expects return value in accumulator A ;; DPTR and R0 should not be modified. ;; This function may safely use R1 without saving ;; Initialize the len variable ;; Load each character from source string’s memory. ;; Q: Which memory? MOV? MOVX? MOVC? ;; Check to make sure it is not the null character ;; If it is null, return the len in A; otherwise, increment count and save the character to the corresponding RAM location. ;; Declare your own labels as needed. TESTSTRING: ;; The purpose is to call STRCPY, fetch the answer ;; (which follows the string data; e.g., STRING1LEN) ;; and compare if they are the same. ;; If the length is different from the expected length ;; then jump to ERROR. ;; Else Compare the copied data with the original (loop over length) ;; If one character does not match ;; then jump to ERROR. ;; Otherwise, increment the global variable t ;; to advance to the next test case ;; You can use CALL pseudocinstruction, and the ;; assembler automatically selects ACALL or LCALL ;; You may trash registers R3 without saving.
MAIN: ;; Use R2 as the variable t, initialize to 0 ;; Load the address of STRING1 to DPTR ;; Call TESTSTRING ;; Load the address of STRING2 to DPTR ;; Call TESTSTRING SUCCESS: SJMP SUCCESS ERROR: SJMP ERROR END
|
You can take a look at the instruction set manual (on Keil’s website) for reference.
When you are done, please turn in the file named hw2.asm at the EEE Drop Box. The EEE Drop Box automatically takes care of renaming it, so please don’t encode your user name or student ID in the file.