程序代写 Assignment 2

Assignment 2

Submission process reminder:

Copyright By PowCoder代写 加微信 powcoder

1. Using your d2l click “Assignments”

2. Click on the name of the assignment

3. Click on “Add a file”

4. Click “Upload” and browse for/select the file to submit then click on add

5. Click “Submit”

6. A message confirming submission should be seen if the submission was successful

7. If you discover error or ambiguity, note this and make the required assumption

8. After submitting your work, please download it again and double-check if you submitted the correct file\files.

Problem 1: (6 marks)
1. What is the value in al in a,b,c, and d? (2 Marks)
mov al,6Bh
shr al,1 a.35h
shl al,3 b.A8h
mov al,8Ch
sar al,1 c.C6h
sar al,3 d.F8h
2. Multiply AX by 26, using shifting and addition instructions. (4 Marks)

;the product of ax*26 is storage in dx, and the final result is 34h(52) in dx when ax=2.

Problem 2: (6 marks)
Using only shifting and addition write a procedure named BitwiseMul that multiplies any unsigned 32-bit integer by EAX. Pass the integer to the procedure in the EBX register, and return the product in the EAX register. Write a test program that calls the procedure and displays the product. (Assume that the product is never larger than 32 bits.)

;Student: ID:1104657
INCLUDE Irvine32.inc
INCLUDE Macros.inc
msg0 byte “Enter # to quit”,0
msg1 byte “Enter first unsigned integer : “,0
msg2 byte “Enter Second unsigned integer: “,0
msg3 byte “The product is:”,0
msg4 byte 0dh,0ah,”_________________________________”,0dh,0ah,0dh,0ah,0
mov edx, offset msg0
call WriteString
call ReadChar
cmp eax,423h
mov edx, offset msg1
call WriteString
call ReadDec
mov ebx,eax ;EAX is multiplier

mov edx, offset msg2;
call WriteString
call ReadDec ; EBX is multiplicand
call BitwiseMul
mov edx, offset msg3;
call WriteString
call WriteDec ; print product
mov edx, offset msg4;
call WriteString

BitwiseMul PROC

mov edx,0 ; set product to zero
mov ecx,32 ; loop counter

L1: test ebx,1 ; check AND statment of ebx and 1 is 1 or not
jz L2 ; if not: skip next to L2
add edx,eax ; if yes: add multiplicand to product

shr ebx,1 ; shift multiplier right
shl eax,1 ; shift multiplicand left
loop L1 ; loop 32 times

L3: mov eax,edx
ret ; return the product
BitwiseMul ENDP

Problem3: (6 Marks)
Write assembly code to find the (GCD) of two integers, and write a test program that calls the function several times, passing it different values. Display all results on the screen. The GCD is the largest integer that will evenly divide both integers. The GCD can be described by the following C++ code:
int GCD(int x, int y)

x = abs(x); // absolute value y = abs(y);

int n = x % y; x = y;
} while (y > 0); return x;

;Student: ID:1104657
INCLUDE irvine32.inc
msg0 byte “The number 1 is:”,0
msg1 byte “The number 2 is:”,0
msg3 byte “The GCD is:”,0
randVal dword ?
mov ecx,10
call Random32
mov edx,offset msg0
call writeString
call writeInt
mov randVal,ebx
call Random32
mov randVal,eax
mov edx,offset msg1
call writeString
call writeInt
call Crlf ;Generate random numbers and print

Imul eax,-1 ;make the first integer positive if number is negative
Imul ebx,-1 ;make the second integer positive if number is negative

jmp TESTING
xor edx,edx ;Save remainder
div ebx ;Quotient save in eax and remainder save in EDX
mov eax, ebx ;num1 = num2
mov ebx, edx ;num2 = num1%num2

jne FORLOOP

mov ebx,eax
mov edx, offset msg3
call writeString
call writeDec ;print GCD
ret ;return

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com