CS代写 | CS-UY 2214 | Homework 9

本次美国代写主要为汇编相关CS的Homework

Introduction
Unless otherwise speci ed, put your answers in a plain text le named hw9.txt. Number each answer.
Submit your work on Gradescope.
The slides, available on Classes, cover many useful Intel opcodes. You may consult the online Intel
assembly language reference to help you understand any unknown opcodes. You can also use the ocial
Intel developer’s manual.
To aid in debugging your assembly language programs, you can use gdb or similar. Please read the
\Reversing tutorial,” available under the \guides” folder in Classes.
Problems
1. Consider the following Intel assembly language fragment. Assume that the label my_data refers to a
region of writable memory.
mov eax , my_data
mov ebx , 0 x0123456
mov [ eax ], ebx
add eax , 2
mov bh , 0 xff
add [ eax ], bh
add eax , 1
mov ecx , 0 xabcdabcd
mov [ eax ], ecx
Give the value of all known memory values starting at my_data. Give your answer as a sequence of
hex bytes. Recall that Intel is a little-endian architecture.
2. Consider the following Intel assembly language program.
section . data
arr :
dw 5, 50, 20, 30, 0
d:
dd 0
section . text
global _start
_start :

mov ecx , arr ; store the address of the array
xor edx , edx
xor eax , eax
loop :
mov dx , [ ecx ] ; read word value
add ecx , 2
sub eax , edx
jns L1 ; jump if sign bit is not set
neg eax ; 2s complement negate
L1:
add [d], eax
mov eax , edx
cmp edx , 0
jne loop
; end of program
(a) What does the program do? What is the nal value of [d]? What is that value in terms of the
initial value of the array arr?
(b) How does the program know when it has reached the end of the array?
(c) What is the size of the values in the array? How do you know?
(d) What is the size of the d variable? How do you know?
(e) What is the total size of the array in bytes?
(f) How does the program calculate absolute value?
(g) At the beginning of the program, we set edx to zero using the xor edx, edx instruction. Why is
this instruction necessary?
3. Consider the following incomplete Intel assembly language program, avg.asm, which is provided in the
hw9.zip le:
section . data
numbers_size :
dd 8
numbers :
dd 10, 34, 55, 106 , 44, 0, 45, 400
section . text
global _start
_start :
; Your code here

Complete the program so that it calculates the integer average of values of the dword array numbers.
The length of the array is stored at address numbers_size. Your program should work correctly with
an array containing any content, of any length. The suggested approach is to rst sum the array, then
divide. The nal result should be stored in eax.

Please examine the documentation for the div instruction. This instruction is slightly anomalous.
Notice that div takes only one argument, the divisor, while the dividend is always implicitly the 64-bit
value in edx:eax (that is, edx forms the most signi cant 32 bits of the dividend, and eax is its least
signi cant 32 bits). The divsor may not be an immediate value (i.e. div 10 won’t work), so you have
to load the divisor into a register. Note, as well, that div ebx will divide the 64-bit value in edx:eax
by ebx, and will store the quotient in eax and the remainder in edx. A consequence of the fact that div
divides a 64-bit value, whose high dword is in edx and whose low dword is in eax, is that, if you want
to divide only eax you must ensure that edx is zero before you issue the instruction. Furthermore, the
quotient must t in the 32-bit register eax: in case of quotient over ow, the CPU will signal a divide
error, and your program will terminate.
Use the Linux exit syscall to terminate your program normally. Specify the array’s average value as
the exit status in ebx, as follows:
mov eax , 1 ; select exit syscall
mov ebx , … ; store the average in ebx
int 80h ; invoke the syscall
You can assemble and link your program with commands similar to those discussed above:
user@ubuntu :/ intel$ nasm -f elf -gstabs avg.asm
user@ubuntu :/ intel$ ld -o avg -m elf i386 avg.o
If your program works, you’ll be able to verify the correct average by examining its exit status, as
follows:
user@ubuntu :/ intel$ ./avg
user@ubuntu :/ intel$ echo $?
86
Submit your complete avg.asm le.