Starter/editDist.c #include #include #include int editDist(char* word1, char* word2); int min(int a, int b); void swap(int** a, int** b); int min(int a, int b){ return a

1 answer below »
This is assembly language programming. I've given you the starter code which covers 50% of the solution.


Starter/editDist.c #include #include #include int editDist(char* word1, char* word2); int min(int a, int b); void swap(int** a, int** b); int min(int a, int b){ return a < b="" a:b;="" }="" void="" swap(int**="" a,="" int**="" b){="" int*="" temp="*a;" *a="*b;" *b="temp;" }="" int="" editdist(char*="" word1,="" char*="" word2){="" int="" word1_len="strlen(word1);" int="" word2_len="strlen(word2);" int*="" olddist="(int*)malloc((word2_len" +="" 1)="" *="" sizeof(int));="" int*="" curdist="(int*)malloc((word2_len" +="" 1)="" *="" sizeof(int));="" int="" i,j,dist;="" intialize="" distances="" to="" length="" of="" the="" substrings="" for(i="0;" i="">< word2_len="" +="" 1;="" i++){="" olddist[i]="i;" curdist[i]="i;" }="" for(i="1;" i="">< word1_len="" +="" 1;="" i++){="" curdist[0]="i;" for(j="1;" j="">< word2_len="" +="" 1;="" j++){="" if(word1[i-1]="=" word2[j-1]){="" curdist[j]="oldDist[j" -="" 1];="" }//the="" characters="" in="" the="" words="" are="" the="" same="" else{="" curdist[j]="min(min(oldDist[j]," deletion="" curdist[j-1]),="" insertion="" olddist[j-1])="" +="" 1;="" subtitution="" }="" }//for="" each="" character="" in="" the="" second="" word="" swap(&olddist,="" &curdist);="" }//for="" each="" character="" in="" the="" first="" word="" dist="oldDist[word2_len];//using" olddist="" instead="" of="" curdist="" because="" of="" the="" last="" swap="" free(olddist);="" free(curdist);="" return="" dist;="" }="" int="" main(int="" argc,="" char**="" argv){="" if(argc="">< 3){ printf("usage: %s word1 word 2\n", argv[0]); exit(1); } printf("the distance between %s and %s is %d.\n", argv[1], argv[2], editdist(argv[1], argv[2])); return 0; } starter/editdist.s starter/makefile sfile = editdist.s exe = editdist.out editdist.out: editdist.o ld -melf_i386 -o $(exe) editdist.o editdist.o: $(sfile) as --32 --gstabs -o editdist.o $(sfile) clean: rm -fr $(exe) editdist.o description files to submit: editdist.s time it took matthew to complete: 20 mins   · all programs must compile without warnings when using the -wall and -werror options · submit only the files requested · do not submit folders or compressed files such as .zip, .rar, .tar, .targz, etc · all input will be valid unless stated otherwise · the examples provided in the prompts do not represent all possible input you can receive. · all inputs in the examples in the prompt are underlined · you don't have to make anything underlined it is just there to help you differentiate between what you are supposed to print and what is being given to your program · if you have questions please post them on campuswire   write an assembly program called editdist.s that calculates the edit distance between 2 strings. you should translate the c code editdist.c, provided in the starter code, to create your solution. an explanation of what edit distance is can be found here while accompanying pseudo code can be found here. 1. the label for the first string should be string1 and the label for the second string should be string2. 2. the edit distance between string1 and string2 should be placed in eax. 3. for each string please allocate space for 100 bytes. 1. while you must allocate space for 100 bytes in your final submission you will likely find it easier to work with the .string directive for testing and debugging. 4. after the last line of code that you wish to be executed in your program please place the label done. 1. make sure that there is an instruction after the done line and a new line after that instruction. if you don't your output won't match mine. 5. i have included a c implementation of the edit distance program. i highly recommend translating this solution into assembly as it will make your life much easier. 1. as a note remember that constants cannot be swapped. pay careful attention to this in your solution 2. use subroutines. it makes life easier (in my opinion) 6. i have included a makefile that will compile your program. your program must be able to be compiled by this makefile when you submit it 7. it is of vital importance that you name your labels as specified and make the appropriate amount of space for each variable! i will be using gdb to test your code and if your labels do not match then the tests will fail. you must also make sure to include the done label after the last line of code you want executed in your program so that i know where to set break points. 3){="" printf("usage:="" %s="" word1="" word="" 2\n",="" argv[0]);="" exit(1);="" }="" printf("the="" distance="" between="" %s="" and="" %s="" is="" %d.\n",="" argv[1],="" argv[2],="" editdist(argv[1],="" argv[2]));="" return="" 0;="" }="" starter/editdist.s="" starter/makefile="" sfile="editDist.s" exe="editDist.out" editdist.out:="" editdist.o="" ld="" -melf_i386="" -o="" $(exe)="" editdist.o="" editdist.o:="" $(sfile)="" as="" --32="" --gstabs="" -o="" editdist.o="" $(sfile)="" clean:="" rm="" -fr="" $(exe)="" editdist.o="" description="" files="" to="" submit:="" editdist.s="" time="" it="" took="" matthew="" to="" complete:="" 20="" mins=""  ="" ·="" all="" programs="" must="" compile="" without="" warnings="" when="" using="" the="" -wall="" and="" -werror="" options="" ·="" submit="" only="" the="" files="" requested="" ·="" do not submit="" folders="" or="" compressed="" files="" such="" as="" .zip,="" .rar,="" .tar,="" .targz,="" etc="" ·="" all="" input="" will="" be="" valid="" unless="" stated="" otherwise="" ·="" the="" examples="" provided="" in="" the="" prompts="" do="" not="" represent="" all="" possible="" input="" you="" can="" receive.="" ·="" all="" inputs="" in="" the="" examples="" in="" the="" prompt="" are="" underlined="" ·="" you="" don't="" have="" to="" make="" anything="" underlined="" it="" is="" just="" there="" to="" help="" you="" differentiate="" between="" what="" you="" are="" supposed="" to="" print="" and="" what="" is="" being="" given="" to="" your="" program="" ·="" if="" you="" have="" questions="" please="" post="" them="" on="" campuswire=""  ="" write="" an="" assembly="" program="" called editdist.s that="" calculates="" the="" edit="" distance="" between="" 2="" strings.="" you="" should="" translate="" the="" c="" code editdist.c, provided="" in="" the="" starter="" code,="" to="" create="" your="" solution. an="" explanation="" of="" what="" edit="" distance="" is="" can="" be="" found here while="" accompanying="" pseudo="" code="" can="" be="" found here.="" 1.="" the="" label="" for="" the="" first="" string="" should="" be string1 and="" the="" label="" for="" the="" second="" string="" should="" be string2.="" 2.="" the="" edit="" distance="" between="" string1="" and="" string2="" should="" be="" placed="" in eax.="" 3.="" for="" each="" string="" please="" allocate="" space="" for="" 100="" bytes.="" 1.="" while="" you="" must="" allocate="" space="" for="" 100="" bytes="" in="" your="" final="" submission="" you="" will="" likely="" find="" it="" easier="" to="" work="" with="" the="" .string="" directive="" for="" testing="" and="" debugging.="" 4.="" after the="" last="" line="" of="" code="" that="" you="" wish="" to="" be="" executed="" in="" your="" program="" please="" place="" the="" label done.="" 1.="" make="" sure="" that="" there="" is="" an="" instruction="" after="" the="" done="" line="" and="" a="" new="" line="" after="" that="" instruction.="" if="" you="" don't="" your="" output="" won't="" match="" mine.="" 5.="" i="" have="" included="" a="" c="" implementation="" of="" the="" edit="" distance="" program.="" i="" highly="" recommend="" translating="" this="" solution="" into="" assembly="" as="" it="" will="" make="" your="" life="" much="" easier.="" 1.="" as="" a="" note="" remember="" that="" constants="" cannot="" be="" swapped.="" pay="" careful="" attention="" to="" this="" in="" your="" solution="" 2.="" use="" subroutines.="" it="" makes="" life="" easier="" (in="" my="" opinion)="" 6.="" i="" have="" included="" a="" makefile="" that="" will="" compile="" your="" program.="" your="" program="" must="" be="" able="" to="" be="" compiled="" by="" this="" makefile="" when="" you="" submit="" it="" 7.="" it="" is="" of="" vital="" importance="" that="" you="" name="" your="" labels="" as="" specified="" and="" make="" the="" appropriate="" amount="" of="" space="" for="" each="" variable! i="" will="" be="" using="" gdb="" to="" test="" your="" code="" and="" if="" your="" labels="" do="" not="" match="" then="" the="" tests="" will="" fail.="" you="" must="" also="" make="" sure="" to="" include="" the="" done="" label="" after="" the="" last="" line="" of="" code="" you="" want="" executed="" in="" your="" program="" so="" that="" i="" know="" where="" to="" set="" break="">
Answered 9 days AfterJul 01, 2021

Answer To: Starter/editDist.c #include #include #include int editDist(char* word1, char* word2); int min(int a,...

Gaurav answered on Jul 05 2021
148 Votes
editDist/editDist.c
#include
#include
#include
int editDist(char* word1, char* word2);
int min(int a, int b);
void swap(int** a, int** b);
int min(int a, int b){
return a < b ? a:b;
}
void swap(int** a, int** b){
int* temp = *a;
*a = *b;
*b = temp;
}
int editDist(char* word1, cha
r* word2){
int word1_len = strlen(word1);
int word2_len = strlen(word2);
int* oldDist = (int*)malloc((word2_len + 1) * sizeof(int));
int* curDist = (int*)malloc((word2_len + 1) * sizeof(int));
int i,j,dist;
//intialize distances to length of the substrings
for(i = 0; i < word2_len + 1; i++){
oldDist[i] = i;
curDist[i] = i;
}
for(i = 1; i < word1_len + 1; i++){
curDist[0] = i;
for(j = 1; j < word2_len + 1; j++){
if(word1[i-1] == word2[j-1]){
curDist[j] = oldDist[j - 1];
}//the characters in the words are the same
else{
curDist[j] = min(min(oldDist[j], //deletion
curDist[j-1]), //insertion
oldDist[j-1]) + 1; //subtitution
}
}//for each character in the second word
swap(&oldDist, &curDist);
}//for each character in the first word
dist = oldDist[word2_len];//using oldDist instead of curDist because of the last swap
free(oldDist);
free(curDist);
return dist;
}
int main(int argc, char** argv){
if(argc < 3){
printf("Usage: %s word1 word 2\n", argv[0]);
exit(1);
}
printf("The distance between %s and %s is %d.\n", argv[1], argv[2], editDist(argv[1], argv[2]));
return 0;
}
editDist/editDist.s
    .globl _start
    .data
string1: .space 100
string2: .space 100
output_string_a: .string "The distance between "
output_string_end_a:
output_string_b: .string " and "
output_string_end_b:
output_string_c: .string " is "
output_string_end_c:
output_string_d: .string ".\n"
output_string_end_d:
usage_string_a: .string "Usage: "
usage_string_end_a:
usage_string_b: .string " word1 word2\n"
usage_string_end_b:
    .text
editDist:
    push %ebp
    movl %esp, %ebp            # save the registers
    
    cld
    xor %al, %al
    xor %ecx, %ecx
    dec %ecx            # ecx = -1
    movl 8(%ebp), %edi        # address of string pushed into the stack by calling function as argmument
    repne scasb            # find the null character in the string and decrement ecx
    not %ecx            # ecx = -ecx
    push %ecx            # save the length of string 1 in the stack
    xor %ecx, %ecx
    dec %ecx            # ecx = -1
    movl 12(%ebp), %edi        # address of string pushed into the stack by calling function as argmument
    repne scasb            # find the null character in the string and decrement ecx
    not %ecx            # ecx = -ecx
    push %ecx            # save the length of string 2 in the stack
    mov -4(%ebp), %eax        # check for empty strings
    mov -8(%ebp), %ebx
    cmp $1, %eax
    je empty_string_1        # if string is empty then return the length of second string
    cmp $1, %ebx
    jne valid_string        # if second string is empty then return the length of first string
empty_string_1:
    cmp %ebx, %eax            
    cmovle %ebx, %eax
    dec %eax            
    jmp exit            # exit the function
valid_string:
    sub $8, %esp            # temporary varibles in the stack to store the address of oldDist, curDist
    mov -4(%ebp), %eax        # length of string1 in word size
    shl $2, %eax            # size in bytes
    sub %eax, %esp            # create space in stack...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here