CS计算机代考程序代写 assembly compiler ## Problems

## Problems

### Q1

The 32-bit base-16 representation of $$24392_{10}$$ is $$5F48_{16}$$, which takes 2 bytes to store. Suppose that we are storing this value on a byte-addressable big endian machine with a 16-bit word size at addresses 550 and 551. Which of the following correctly shows the contents of bytes 550 and 551 in memory, in hexadecimal?

( ) Address 550: $$5F_{16}$$; address 551: $$48_{16}$$.
( ) Address 550: $$48_{16}$$; address 551: $$5F_{16}$$.
( ) Address 550: $$F5_{16}$$; address 551: $$84_{16}$$.
( ) Address 550: $$84_{16}$$; address 551: $$F5_{16}$$.

### Q2

Explain why the instruction `add (%ebx), (%ecx)` is not allowed.

|____|

### Q3

Recall that in a C program, a pointer contains as its value the address of another piece of data. Where is this piece of data? Select one of the two answers below and justify your answer.

( ) Main memory.
( ) Secondary storage.

|____|

### Q4

Suppose that the designers of the C programming language introduced an unsigned integer type called `aaron` that supported values in the range $$0_{10}$$ to $$47_{10}$$. What would be the *minimum* number of bits that this type needs in order to support its range of values? (Assume that the number of bits need not be a multiple of 8.) Justify your answer.

|____|

### Q5

Consider the following statically allocated multidimensional array.

“` C
int arr[][3] = {{92, 49, 31},
{22, 57, 61},
{18, 93, 53},
{14, 17, 25}};
“`

In what order would the elements of the above array be stored if column-major ordering were used?

( ) 92, 49, 31, 22, 57, 61, 18, 93, 53, 14, 17, 25
( ) 92, 57, 53, 14, 49, 61, 18, 17, 31, 22, 93, 25
( ) 92, 49, 31, 61, 57, 22, 18, 93, 53, 25, 17, 14
( ) 92, 22, 18, 14, 17, 93, 57, 49, 31, 61, 53, 25
( ) None of the above.

### Q6

Consider the C file `main.c` shown below.

“` C
#include “foo.h”

int main()
{
}
“`

Which of the following is an accurate description of how `foo.h` is dealt with when `gcc` (the C compiler) is run on `main.c`.

1. The contents of `foo.h` — C code — are loaded into `main.c`.
2. The contents of `foo.h` are first converted into assembly code *and then* loaded into `main.c`.

Justify your answer.

|____|

### Q7

After the below assembly code is executed, the value of EAX is 0 and not 25. Why?

“` gas
mov $25, %eax
mov $55, %ebx
mov $89, %r8d
mov $0, %r9d
mov $0, %rax
mov $0, %rdx
“`

|____|

### Q8

Below is an x86-64 assembly program using AT&T syntax that is supposed to determine the number of negative values in the `arr` array and place this count in the `count` variable. The program does not assume that the the length of the array (which is given by the `arrlen` variable) or the array’s contents will always be the same. The program behaves correctly, but it may not be as efficient as it could be.

Don’t worry about what happens in the program after the last line shown below. Also, in some cases, I had to use an instruction suffix.

*Your task is mentioned after the code.*

“` gas
.data

######
# arr and arrlen are considered the inputs to the program.
######

arr:
.long 5
.long -3
.long 12
.long -20
.long -2
.long 19
.long 14
.long -27

arrlen:
.long 8

######
# Final result goes in count.
######

count:
.long 0

######
# The below variables are to help the program do its job.
######

lenInBytes:
.long 0

currIndex:
.long 0

.text
.globl _start
_start:
mov arrlen, %eax
imul $4, %eax
mov %eax, lenInBytes
movl $0, currIndex
movl $0, count
loop:
mov currIndex, %eax
cmp %eax, lenInBytes
jle done
cmp $0, arr(%eax)
jge endIter
incl count
endIter:
cmp %eax, lenInBytes
jmp endIter2
endIter2:
add $4, currIndex
jmp loop
done:
nop
“`

Explain all ways in which the program can be modified to be faster.

|____|

### Q9

Write an x86-64 assembly language program using AT&T syntax that has the five variables `a`, `b`, `c`, `d`, and `result` shown below (with example values). The program should set `result` to 1 if the sum of the values in `a` and `b` is greater than the sum of the values in `c` and `d`. Otherwise, `result` should be set to `0`.

Below is a skeleton version to help get you started. With the example values, the sum of the values in `a` and `b` is 15, whereas the sum of the values in `c` and `d` is 12, so `result` would be set to 1. The program should end at the `nop` instruction at the end.

**Your program cannot be more than 18 instructions**, which should be more than enough. This does not count labels or the `nop`.

*Hint*: If you place a value (e.g. 3) directly in a variable, you will have to use an instruction suffix. For example, `mov $1, result` does not work, but `movl $1, result` does.

“` gas
.data

a:
.long 5

b:
.long 10

c:
.long 3

d:
.long 9

result:
.long -1

.text
.globl _start
_start:
# TODO: Implement.

done:
nop
“`

|____|

|files|

### Q10

Suppose that we want to check if two integers have any of the same bits set (to 1). For example, $$4_{10}$$ ($$100_2$$) and $$9_{10}$$ ($$1001_2$$) do not have any of the same bits set, but $$7_{10}$$ ($$111_2$$) and $$9_{10}$$ ($$1001_2$$) do. Which *one* of the following bitwise operations can best help in accomplishing the desired task? Justify your answer in the text box below.

( ) Bitwise AND.
( ) Bitwise OR.
( ) Bitwise XOR.
( ) Bitwise NOT.

|____|

### Q11

*This question has been removed.*

### Q12

Below is an x86-64 assembly program using AT&T syntax in a file called `main.s`.

“` gas
.data

a:
.long 8

b:
.long 10

c:
.long -1

.text
.globl _start
_start:
mov a, %edx
imul b, %edx
mov %edx, %eax
mov $500, %ebx
idiv %ebx
mov %eax, c
done:
nop
“`

The program is supposed to multiply the values in the `a` and `b` variables together and then divide this product by 500 and place the result in the `c` variable. As shown below, the program does not behave properly; the value of `c` should have been 0 at the end, not 687194767.

“`
$ as –gstabs main.s -o main.o
$ ld main.o -o main
$ gdb ./main

(gdb) b done
Breakpoint 1 at 0x4000cf: file main.s, line 22.
(gdb) r
Starting program: /home/…/main

Breakpoint 1, done () at main.s:22
22 nop
(gdb) p (int) c
$1 = 687194767
“`

Explain why the program is misbehaving and how to fix the program.

|____|

### Q13

If the CPU places the value 8500 in the MAR and “tree” in the MDR, then which of the following is the CPU trying to do?

( ) Read the value “tree” from address 8500 in main memory.
( ) Write the value “tree” to address 8500 in main memory.
( ) Read the value “tree” from address 8500 in secondary storage.
( ) Write the value “tree” to address 8500 in secondary storage.
( ) Read whatever value is at address 8500 in main memory and place it in a variable named “tree”.

*Based on your answer above*, which of the following two lines would be asserted?

( ) Read control line.
( ) Write control line.

### Q14

Which of the following is not a component of the traditional Von Neumann architecture?

( ) CPU.
( ) Main memory.
( ) GPU.
( ) I/O facility.

## Copyright Statement

This content is protected and may not be shared, uploaded, or distributed.