Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Suppose we were to call a procedure named ProcOne , passing it two value para

ID: 3532674 • Letter: 1

Question

1. Suppose we were to call a procedure named ProcOne , passing it two value parameters. The procedure adds the two parameters and returns their sum in EAX: push 5 push 10 call ProcOne ; EAX = sum Create the implementation of ProcOne , using explicit stack parameters (such as [ebp+n]). Follow other common conventions regarding the saving and restoring of registers. Restore the stack pointer using the STDCALL calling convention, and assume a 32 - bit me mory model. 2. Suppose we were to call a procedure named ProcTwo , which calculates the sum of the elements of an array of 16 - bit words and returns the sum in EAX. The following code calls ProcTwo : .data myArray WORD 100h,200h,300h .code push LENGTHOF myArray push OFFSET myArray call ProcTwo ; EAX = sum Write the implementation of ProcTwo , using explicit stack parameters (such as [ebp+n]). Follow other common conventions regarding the saving and restoring of registers. Restore the stack pointer using the PASC AL calling convention, and assume a 32 - bit memory model. 3. Write a series of statements that would typically be found inside a procedure. Do the following: create space for a single doubleword local variable on the stack, and assign a random 32 - bit integer to the variable.



4. Using the following definition of a two - dimensional array, write a sequence of instructions that use a base - index operand to move the element at row 2, column 3 to AX. (Row and column numbers begin at 0): .data ROWSIZE = 5 NUMROWS = 4 twArray WORD NUMROWS DUP( ROWSIZE DUP(?) ) 5. Using the following data definitions, write instructions that use MOVSB to copy all the bytes from source to target : .data source BYTE 50 DUP(?) target BYTE 100 DUP(?) 6. Write a sequence of instruct ions using MOVSB that append source to the end of target . The first three instructions have been written for you: .data source BYTE "HIJKLMNOPQRS",0 target BYTE "ABCDEFG",50 DUP(0) .code INVOKE Str_length, ADDR target mov edi,OFFSET target add edi,eax 7. W rite a sequence of instructions that use SCASD to search arrayD for the first value that is not the same as the current contents of EAX. When the search is completed, move the value you have found into EAX: arrayD SDWORD 50 DUP(?) 8. Write a sequence of instr uctions that use STOSD to fill each position of arrayD with the largest possible 32 - bit positive integer: arrayD SDWORD 20 DUP(?) 9. Write a sequence of instructions using CMPSD that compare arrayA to arrayB in reverse order. After the comparison, move the fi rst pair of non - matching array values to EAX and EBX. COUNT = 5 arrayA DWORD COUNT DUP(?) arrayB DWORD COUNT DUP(?) 10. Write a sequence of instructions using LODSB and STOSB that copy each character from arrayA to arrayB, converting it to lowercase in the process. Note: setting bit 5 in an uppercase character will convert it to lowercase: arrayA BYTE "ABCDEFGHI" arrayB BYTE LENGTHOF arrayA DUP(0)


11. Create a macro named putChar that writes a single character, passed as a macro argument, to standard output. Be sure to push and pop any registers modified by the macro. 12. Create a macro named mMove16 that moves any 16 - bit memory operand to any other 16 - bit memory operand. Save and restore any registers modified by the macro. Syntax: mMove destination,source 13. Create a mac ro named mMult16 that multiplies any two signed 16 - bit memory operands and produces a 16 - bit product. Syntax: mMult16 product,op1,op2 14. Create a macro named mWrite Hex that writes the value of a 32 - bit memory operand to standard output in hexadecimal format. 15. Using the mWriteHex macro from the previous question, write a statement tha t invokes the macro, using ESI as an indirect operand. 16. Create a macro named mWriteHexConst that lets the caller pass an integer constant argument. The integer is written to standard output in hexadecimal format. All labels must be local. 17. Create a macro named LongLoop that is not constrained by the LOOP instruction's limited ? 128 to +127 byte range. All labels must b e local. 18. Write a macro named mShowValue that writes a literal string to standard output, followed by the contents of an integer variable in unsigned decimal. Push and pop any reg isters modified by the macro. All labels must be local. Following is a sample call: .data salary DWORD 52100 .code mShowValue "Salary is equal to: ", salary 19. Write a sequence of statements that exits a macro if the count argument is blank. 20. Write a sequence of statements that displays an error mess age on the console during assembly if the first macro argument named count is identical to "ECX". Make the comparison case - insensitive. 21. Write a macro named myMac that has a single paramet er named first , which is given a default argument initializer of 0. The macro should move first to the EAX register. 22. The mWrite macro shown in Chapter 10 displays a string literal on the console. A sample call is: mWrite "Hello there" Show how to call mWrite and pass it a string that contains both ordinary characters and carriage - return/linefeed bytes (0Dh, 0Ah). 23. Use the REPEAT directive to define an array named myArray that conta ins 50 16 - bit unsigned words, with initial values based on the following sequence: {5,7,9,...,103} 24. Use the FOR directive to create five uninitialized DWORD variables in which each varia ble name is a member of the following list: monday,tuesday,wednesday,thursday,friday. 25. Write down the contents of the first five rows (in hexadecimal) of the table generated by th e following statements: count = 0 FORC digit,<0123456789ABCDEF> BYTE count,"&digit" count = count + 1 ENDM
1. Suppose we were to call a procedure named ProcOne , passing it two value parameters. The procedure adds the two parameters and returns their sum in EAX: push 5 push 10 call ProcOne ; EAX = sum Create the implementation of ProcOne , using explicit stack parameters (such as [ebp+n]). Follow other common conventions regarding the saving and restoring of registers. Restore the stack pointer using the STDCALL calling convention, and assume a 32 - bit me mory model. 2. Suppose we were to call a procedure named ProcTwo , which calculates the sum of the elements of an array of 16 - bit words and returns the sum in EAX. The following code calls ProcTwo : .data myArray WORD 100h,200h,300h .code push LENGTHOF myArray push OFFSET myArray call ProcTwo ; EAX = sum Write the implementation of ProcTwo , using explicit stack parameters (such as [ebp+n]). Follow other common conventions regarding the saving and restoring of registers. Restore the stack pointer using the PASC AL calling convention, and assume a 32 - bit memory model. 3. Write a series of statements that would typically be found inside a procedure. Do the following: create space for a single doubleword local variable on the stack, and assign a random 32 - bit integer to the variable.



4. Using the following definition of a two - dimensional array, write a sequence of instructions that use a base - index operand to move the element at row 2, column 3 to AX. (Row and column numbers begin at 0): .data ROWSIZE = 5 NUMROWS = 4 twArray WORD NUMROWS DUP( ROWSIZE DUP(?) ) 5. Using the following data definitions, write instructions that use MOVSB to copy all the bytes from source to target : .data source BYTE 50 DUP(?) target BYTE 100 DUP(?) 6. Write a sequence of instruct ions using MOVSB that append source to the end of target . The first three instructions have been written for you: .data source BYTE "HIJKLMNOPQRS",0 target BYTE "ABCDEFG",50 DUP(0) .code INVOKE Str_length, ADDR target mov edi,OFFSET target add edi,eax 7. W rite a sequence of instructions that use SCASD to search arrayD for the first value that is not the same as the current contents of EAX. When the search is completed, move the value you have found into EAX: arrayD SDWORD 50 DUP(?) 8. Write a sequence of instr uctions that use STOSD to fill each position of arrayD with the largest possible 32 - bit positive integer: arrayD SDWORD 20 DUP(?) 9. Write a sequence of instructions using CMPSD that compare arrayA to arrayB in reverse order. After the comparison, move the fi rst pair of non - matching array values to EAX and EBX. COUNT = 5 arrayA DWORD COUNT DUP(?) arrayB DWORD COUNT DUP(?) 10. Write a sequence of instructions using LODSB and STOSB that copy each character from arrayA to arrayB, converting it to lowercase in the process. Note: setting bit 5 in an uppercase character will convert it to lowercase: arrayA BYTE "ABCDEFGHI" arrayB BYTE LENGTHOF arrayA DUP(0)


11. Create a macro named putChar that writes a single character, passed as a macro argument, to standard output. Be sure to push and pop any registers modified by the macro. 12. Create a macro named mMove16 that moves any 16 - bit memory operand to any other 16 - bit memory operand. Save and restore any registers modified by the macro. Syntax: mMove destination,source 13. Create a mac ro named mMult16 that multiplies any two signed 16 - bit memory operands and produces a 16 - bit product. Syntax: mMult16 product,op1,op2 14. Create a macro named mWrite Hex that writes the value of a 32 - bit memory operand to standard output in hexadecimal format. 15. Using the mWriteHex macro from the previous question, write a statement tha t invokes the macro, using ESI as an indirect operand. 16. Create a macro named mWriteHexConst that lets the caller pass an integer constant argument. The integer is written to standard output in hexadecimal format. All labels must be local. 17. Create a macro named LongLoop that is not constrained by the LOOP instruction's limited ? 128 to +127 byte range. All labels must b e local. 18. Write a macro named mShowValue that writes a literal string to standard output, followed by the contents of an integer variable in unsigned decimal. Push and pop any reg isters modified by the macro. All labels must be local. Following is a sample call: .data salary DWORD 52100 .code mShowValue "Salary is equal to: ", salary 19. Write a sequence of statements that exits a macro if the count argument is blank. 20. Write a sequence of statements that displays an error mess age on the console during assembly if the first macro argument named count is identical to "ECX". Make the comparison case - insensitive. 21. Write a macro named myMac that has a single paramet er named first , which is given a default argument initializer of 0. The macro should move first to the EAX register. 22. The mWrite macro shown in Chapter 10 displays a string literal on the console. A sample call is: mWrite "Hello there" Show how to call mWrite and pass it a string that contains both ordinary characters and carriage - return/linefeed bytes (0Dh, 0Ah). 23. Use the REPEAT directive to define an array named myArray that conta ins 50 16 - bit unsigned words, with initial values based on the following sequence: {5,7,9,...,103} 24. Use the FOR directive to create five uninitialized DWORD variables in which each varia ble name is a member of the following list: monday,tuesday,wednesday,thursday,friday. 25. Write down the contents of the first five rows (in hexadecimal) of the table generated by th e following statements: count = 0 FORC digit,<0123456789ABCDEF> BYTE count,"&digit" count = count + 1 ENDM
1. Suppose we were to call a procedure named ProcOne , passing it two value parameters. The procedure adds the two parameters and returns their sum in EAX: push 5 push 10 call ProcOne ; EAX = sum Create the implementation of ProcOne , using explicit stack parameters (such as [ebp+n]). Follow other common conventions regarding the saving and restoring of registers. Restore the stack pointer using the STDCALL calling convention, and assume a 32 - bit me mory model. 2. Suppose we were to call a procedure named ProcTwo , which calculates the sum of the elements of an array of 16 - bit words and returns the sum in EAX. The following code calls ProcTwo : .data myArray WORD 100h,200h,300h .code push LENGTHOF myArray push OFFSET myArray call ProcTwo ; EAX = sum Write the implementation of ProcTwo , using explicit stack parameters (such as [ebp+n]). Follow other common conventions regarding the saving and restoring of registers. Restore the stack pointer using the PASC AL calling convention, and assume a 32 - bit memory model. 3. Write a series of statements that would typically be found inside a procedure. Do the following: create space for a single doubleword local variable on the stack, and assign a random 32 - bit integer to the variable.



4. Using the following definition of a two - dimensional array, write a sequence of instructions that use a base - index operand to move the element at row 2, column 3 to AX. (Row and column numbers begin at 0): .data ROWSIZE = 5 NUMROWS = 4 twArray WORD NUMROWS DUP( ROWSIZE DUP(?) ) 5. Using the following data definitions, write instructions that use MOVSB to copy all the bytes from source to target : .data source BYTE 50 DUP(?) target BYTE 100 DUP(?) 6. Write a sequence of instruct ions using MOVSB that append source to the end of target . The first three instructions have been written for you: .data source BYTE "HIJKLMNOPQRS",0 target BYTE "ABCDEFG",50 DUP(0) .code INVOKE Str_length, ADDR target mov edi,OFFSET target add edi,eax 7. W rite a sequence of instructions that use SCASD to search arrayD for the first value that is not the same as the current contents of EAX. When the search is completed, move the value you have found into EAX: arrayD SDWORD 50 DUP(?) 8. Write a sequence of instr uctions that use STOSD to fill each position of arrayD with the largest possible 32 - bit positive integer: arrayD SDWORD 20 DUP(?) 9. Write a sequence of instructions using CMPSD that compare arrayA to arrayB in reverse order. After the comparison, move the fi rst pair of non - matching array values to EAX and EBX. COUNT = 5 arrayA DWORD COUNT DUP(?) arrayB DWORD COUNT DUP(?) 10. Write a sequence of instructions using LODSB and STOSB that copy each character from arrayA to arrayB, converting it to lowercase in the process. Note: setting bit 5 in an uppercase character will convert it to lowercase: arrayA BYTE "ABCDEFGHI" arrayB BYTE LENGTHOF arrayA DUP(0)


11. Create a macro named putChar that writes a single character, passed as a macro argument, to standard output. Be sure to push and pop any registers modified by the macro. 12. Create a macro named mMove16 that moves any 16 - bit memory operand to any other 16 - bit memory operand. Save and restore any registers modified by the macro. Syntax: mMove destination,source 13. Create a mac ro named mMult16 that multiplies any two signed 16 - bit memory operands and produces a 16 - bit product. Syntax: mMult16 product,op1,op2 14. Create a macro named mWrite Hex that writes the value of a 32 - bit memory operand to standard output in hexadecimal format. 15. Using the mWriteHex macro from the previous question, write a statement tha t invokes the macro, using ESI as an indirect operand. 16. Create a macro named mWriteHexConst that lets the caller pass an integer constant argument. The integer is written to standard output in hexadecimal format. All labels must be local. 17. Create a macro named LongLoop that is not constrained by the LOOP instruction's limited ? 128 to +127 byte range. All labels must b e local. 18. Write a macro named mShowValue that writes a literal string to standard output, followed by the contents of an integer variable in unsigned decimal. Push and pop any reg isters modified by the macro. All labels must be local. Following is a sample call: .data salary DWORD 52100 .code mShowValue "Salary is equal to: ", salary 19. Write a sequence of statements that exits a macro if the count argument is blank. 20. Write a sequence of statements that displays an error mess age on the console during assembly if the first macro argument named count is identical to "ECX". Make the comparison case - insensitive. 21. Write a macro named myMac that has a single paramet er named first , which is given a default argument initializer of 0. The macro should move first to the EAX register. 22. The mWrite macro shown in Chapter 10 displays a string literal on the console. A sample call is: mWrite "Hello there" Show how to call mWrite and pass it a string that contains both ordinary characters and carriage - return/linefeed bytes (0Dh, 0Ah). 23. Use the REPEAT directive to define an array named myArray that conta ins 50 16 - bit unsigned words, with initial values based on the following sequence: {5,7,9,...,103} 24. Use the FOR directive to create five uninitialized DWORD variables in which each varia ble name is a member of the following list: monday,tuesday,wednesday,thursday,friday. 25. Write down the contents of the first five rows (in hexadecimal) of the table generated by th e following statements: count = 0 FORC digit,<0123456789ABCDEF> BYTE count,"&digit" count = count + 1 ENDM

4. Using the following definition of a two - dimensional array, write a sequence of instructions that use a base - index operand to move the element at row 2, column 3 to AX. (Row and column numbers begin at 0): .data ROWSIZE = 5 NUMROWS = 4 twArray WORD NUMROWS DUP( ROWSIZE DUP(?) ) 5. Using the following data definitions, write instructions that use MOVSB to copy all the bytes from source to target : .data source BYTE 50 DUP(?) target BYTE 100 DUP(?) 6. Write a sequence of instruct ions using MOVSB that append source to the end of target . The first three instructions have been written for you: .data source BYTE "HIJKLMNOPQRS",0 target BYTE "ABCDEFG",50 DUP(0) .code INVOKE Str_length, ADDR target mov edi,OFFSET target add edi,eax 7. W rite a sequence of instructions that use SCASD to search arrayD for the first value that is not the same as the current contents of EAX. When the search is completed, move the value you have found into EAX: arrayD SDWORD 50 DUP(?) 8. Write a sequence of instr uctions that use STOSD to fill each position of arrayD with the largest possible 32 - bit positive integer: arrayD SDWORD 20 DUP(?) 9. Write a sequence of instructions using CMPSD that compare arrayA to arrayB in reverse order. After the comparison, move the fi rst pair of non - matching array values to EAX and EBX. COUNT = 5 arrayA DWORD COUNT DUP(?) arrayB DWORD COUNT DUP(?) 10. Write a sequence of instructions using LODSB and STOSB that copy each character from arrayA to arrayB, converting it to lowercase in the process. Note: setting bit 5 in an uppercase character will convert it to lowercase: arrayA BYTE "ABCDEFGHI" arrayB BYTE LENGTHOF arrayA DUP(0) 11. Create a macro named putChar that writes a single character, passed as a macro argument, to standard output. Be sure to push and pop any registers modified by the macro. 12. Create a macro named mMove16 that moves any 16 - bit memory operand to any other 16 - bit memory operand. Save and restore any registers modified by the macro. Syntax: mMove destination,source 13. Create a mac ro named mMult16 that multiplies any two signed 16 - bit memory operands and produces a 16 - bit product. Syntax: mMult16 product,op1,op2 14. Create a macro named mWrite Hex that writes the value of a 32 - bit memory operand to standard output in hexadecimal format. 15. Using the mWriteHex macro from the previous question, write a statement tha t invokes the macro, using ESI as an indirect operand. 16. Create a macro named mWriteHexConst that lets the caller pass an integer constant argument. The integer is written to standard output in hexadecimal format. All labels must be local. 17. Create a macro named LongLoop that is not constrained by the LOOP instruction's limited ? 128 to +127 byte range. All labels must b e local. 18. Write a macro named mShowValue that writes a literal string to standard output, followed by the contents of an integer variable in unsigned decimal. Push and pop any reg isters modified by the macro. All labels must be local. Following is a sample call: .data salary DWORD 52100 .code mShowValue "Salary is equal to: ", salary 19. Write a sequence of statements that exits a macro if the count argument is blank. 20. Write a sequence of statements that displays an error mess age on the console during assembly if the first macro argument named count is identical to "ECX". Make the comparison case - insensitive. 21. Write a macro named myMac that has a single paramet er named first , which is given a default argument initializer of 0. The macro should move first to the EAX register. 22. The mWrite macro shown in Chapter 10 displays a string literal on the console. A sample call is: mWrite "Hello there" Show how to call mWrite and pass it a string that contains both ordinary characters and carriage - return/linefeed bytes (0Dh, 0Ah). 23. Use the REPEAT directive to define an array named myArray that conta ins 50 16 - bit unsigned words, with initial values based on the following sequence: {5,7,9,...,103} 24. Use the FOR directive to create five uninitialized DWORD variables in which each varia ble name is a member of the following list: monday,tuesday,wednesday,thursday,friday. 25. Write down the contents of the first five rows (in hexadecimal) of the table generated by th e following statements: count = 0 FORC digit,<0123456789ABCDEF> BYTE count,"&digit" count = count + 1 ENDM

Explanation / Answer

1.
ProcOne PROC push ebp mov ebp,esp

mov eax,[ebp+12]

add eax,[ebp+8]

; mov [ebp+8],eax

mov esp,ebp

pop ebp

ret

ProcOne ENDP

2.

ProcTwo PROC

push ebp mov ebp,esp

mov esi,[ebp+8]

mov ecx,[ebp+12]

mov eax,0

sum: add eax,[esi]

add esi,4

loop sum

mov esp,ebp

pop ebp

ret ProcTwo END

3.

sub esp,4

call Random32

mov [ebp-4],eax

4.

.data

ROWSIZE = 5

NUMROWS = 4

twArray WORD NUMROWS DUP(ROWSIZE DUP(?))

.code

;(first 2 statements may be in any order)

mov esi,ROWSIZE * 2

add OFFSET, twArray ; This instruction can be removed

mov edi,3

mov ax,[esi+edi]
5.

.data

source BYTE "HIJKLMNOPQRS",0

target BYTE "ABCDEFG",50 DUP(0)

.code

INVOKE Str_length, ADDR target

mov edi,OFFSET target

add edi,eax

;(first 3 statements may be in any order)

mov esi,OFFSET source

mov ecx,SIZEOF source

cld

rep movsb
6.

.data

source BYTE "HIJKLMNOPQRS",0

target BYTE "ABCDEFG",50 DUP(0)

.code

INVOKE Str_length, ADDR target

mov edi,OFFSET target

add edi,eax

;(first 3 statements may be in any order)

mov esi,OFFSET source

mov ecx,SIZEOF source

cld

rep movsb
8.

;(first 4 statements may be in any order)

mov eax,7FFFFFFFh

mov edi,OFFSET arrayD

mov ecx,LENGTHOF arrayD

cld

rep stosd