Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

convert the following c file into assembly using the PDF provided Intel Cheat Sheet PDF Registers Each of the following registers is 32 bits in

convert the following c file into assembly using the PDF provided

Intel Cheat Sheet PDF

Registers Each of the following registers is 32 bits in size Name Meaning of Acronym Usage EAX Accumulator General Purpose Register EBX Base General Purpose Register ECX Counter General Purpose Register EDX Data General Purpose Register ESI Source Index General Purpose Register EDI Destination Index General Purpose Register EBP Base Pointer Used as the base pointer in C code. Can be used as a general register in stand alone code ESP Stack Pointer The stack pointer. You can access smaller portions of EAX, EBX, ECX, and EDX The following example only shows the syntax for eax but it does apply to the the above mentioned registers Syntax Size EAX 32 bits AX Lower 16 bits of EAX AH The upper 8 bits of AX AL The lower 8 bits of AX AS Preprocesser Memory Allocations Syntax Effect Example .byte val Make space for a byte and initialize it val .byte 'h' .word val Make space for 2 bytes and initialize it val .word 12 .long val Make space for 4 byte and initialize it val .long 1024 .float val Make space for 4 bytes and initialize its value to the IEE floating point standard .float 58.45 .string val Make space for null terminated string and initialize it to val. .string Rock the boat .space N Make space for N bytes of uninitialized memory .space 16 GAS Preprocessor Directives Syntax Effect Example .equ label, value Defines label to be value. All occurrences of label will be substituted with value .equ wordsize, 4 .data Marks the beginning of the data section .text Marks the beginning of the instruction section label_name: Creates the label with name label_name hello_world: .global label Make label visible to linker .global _start .rept N code .endr Repeat all code N times .rept 4 addl %eax, %eax .endr _start: While not actually a preprocessor directive _start indicates where execution should begin. It is equivalent to main in C Operations In operations that require two operands the left is called the source and the right the destination Operation Result Affects Flag Register? mov src, dest dest = src No add src, dest dest = dest + src Yes sub src, dest dest = dest - src Yes cmp src, dest dest src (result not stored) Yes inc dest dest = dest + 1 Yes dec dest dest = dest - 1 Yes and src, dest dest = dest & src Yes or src, dest dest = dest | src Yes xor src, dest Dest = dest ^ src Yes not dest dest = ~dest Yes shr src, dest dest = dest >> src (brings in 0s) Yes sar src, dest dest = dest >> src (brings in bit with same value as MSB Yes shl src, dest dest = dest << src Yes sal src, dest dest = dest << src (identical to shl) Yes Constants can be specified for the source by placing a $ in front of the value ie addl $4, %ecx All of the above operations need to have one of the following suffixes appended to them to determine the size of the unit they are operating on R is standing in for the variable name of each register ie (A,B,C,D) Suffix Size Register Portion To Use l (this is the letter L not 1) 32 bits ERX w 16 bits RX b byte RL Multiplication and Division Operation Result Type Affects Flag Register? mulb src ax = al * src unsgined Yes mulw src dx:ax = ax * src unsgined Yes mull src edx:eax = eax * src unsgined Yes divb src ah = al / src al = ax % src unsgined Yes divw src ax = dx:ax / src dx = dx:ax % src unsgined Yes divl src eax = edx:eax / src edx = edx:eax % src unsgined Yes EDX:EAX means to concatenate the bits in EAX and EDX together and treat it as a 64 bit number To do signed multiplication/division append an I to the front of the instruction ie(imull, idivl) String Instructions Operation Result ECX EDI ESI movsb C(%edi) = C(%esi) ECX-- If Direction EDI -= 1 else EDI += 1 If Direction ESI -= 1 else ESI += 1 movsw C(%edi) = C(%esi) ECX-- If Direction EDI -= 2 else EDI += 2 If Direction ESI -= 2 else ESI += 2 movsl C(%edi) = C(%esi) ECX-- If Direction EDI -= 4 else EDI += 4 If Direction ESI -= 4 else ESI += 4 stosb C(%edi) = AL ECX-- If Direction EDI -= 1 else EDI += 1 Unaffected stosw C(%edi) =AX ECX-- If Direction EDI -= 2 else EDI += 2 Unaffected stosl C(%edi) = EAX ECX-- If Direction EDI -= 4 else EDI += 4 Unaffected cmpsb C(%esi) - C(%edi) ECX-- If Direction EDI -= 1 else EDI += 1 If Direction ESI -= 1 else ESI += 1 cmpsw C(%esi) - C(%edi) ECX-- If Direction EDI -= 2 else EDI += 2 If Direction ESI -= 2 else ESI += 2 cmpsl C(%esi) - C(%edi) ECX-- If Direction EDI -= 4 else EDI += 4 If Direction ESI -= 4 else ESI += 4 scasb AL - C(%edi) ECX-- If Direction EDI -= 1 else EDI += 1 Unaffected scasw AX - C(%edi) ECX-- If Direction EDI -= 2 else EDI += 2 Unaffected scasl EAX - C(%edi) ECX-- If Direction EDI -= 4 else EDI += 4 Unaffected Direction is controlled by the direction flag. To set the direction flag use the instruction STD. To clear the direction flag use CLD Repetition Prefixes Operation Supported Prefixes movs rep stos rep cmps rep, repe, repne scas rep, repe, repne Appending rep to any of the instructions mentioned causes them to continue to execute until ECX reaches 0 repe (repeat while equal) causes the instruction to be repeated until either ECX reaches 0 or the comparison results in an inequality repne (repeat while not equal) causes the instruction to be repeated until either ECX reaches 0 or the comparison results in an equality For repe and repne to check what caused the termination of the instruction you can use JECXZ: Jump if ECX is 0, to check if ECX's value is 0 JZ/JNZ: to check if the comparison resulted in an equality/inequality Jumps Instruction Description Comparison Type jmp label Unconditional jump NA jl label Jump if less than 0 Signed jg label Jump if greater than 0 Signed jle label Jump if less than or equal 0 Signed jge label Jump if greater than or equal 0 Signed jb label Jump if below 0 Unsigned ja label Jump if above 0 Unsigned jbe label Jump if below or equal 0 Unsigned jae label Jump if above or equal 0 Unsigned jz label Jump if zero Signed or Unsigned jnz label Jump if not zero Signed or Unsigned jc label Jump if carry NA jnc label Jump if no carry NA jo label Jump if overflow NA jno label Jump if no overflow NA Jumps are used to control the flow of execution All jumps are based off the last instruction to set the sign flag Stack Operations Operation Result Affects Flag Register? pushl src C(esp) = src, esp -= 4 No popl dest dest = C(esp), esp += 4 No C means the value of the contents of memory at the given All operations to the stack must be of word size so you can't do pushw, popb, etc Call and Return Operation Result Call label Push PC, jmp label ret Pop PC

#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 ", argv[0]); exit(1); } printf("The distance between %s and %s is %d. ", argv[1], argv[2], editDist(argv[1], argv[2]));

return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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