Assembly language Problem(And could you please attach a screenshot of your outpu
ID: 3830801 • Letter: A
Question
Assembly language Problem(And could you please attach a screenshot of your output)
Part A 4. Encryption Using Rotate Operations Write a program that performs simple encryption by rotating each plaintext byte a varying num- ber of positions in different directions. For example, in the following array that represents the encryption key, a negative value indicates a rotation to the left and a positive value indicates a rotation to the right. The integer in each position indicates the magnitude of the rotation: key BYTE -2, 4, 1, 0, -3, 5, 2, -4, -4, 6 Your program should loop through a plaintext message and align the key to the first 10 bytes of the message. Rotate each plaintext byte by the amount indicated by its matching key amay value. Then, align the key to the next 10 bytes of the message and repeat the process. (A VideoNote for this exercise is posted on the Web site.) Part B 6. Greatest Common Divisor (GCD) The greatest common divisor (GCD) of two integers is the largest integer that will evenly divide both integers. The GCD algorithm involves integer division in a loop, described by the following C++ code: int GCD (int x, int y) x abs (x) absolute value y abs (y) do int n x 8 y y E n; while (y 0); return x; Implement this function in assembly language and write a test program that calls the function several times, passing it different values. Display all results on the screen.Explanation / Answer
1) Assembly language code for Encryption using rotate operations.
INCLUDE Irvine32.inc
INCLUDE Macros.inc
BufSize = 80
.data
key BYTE 6, 4, 1, 2, 7, 5, 2, 4, 3, 6
myText BYTE "This text is going to be encrypted.", 0
buffer BYTE BufSize DUP(?),0,0
stdInHandle HANDLE ?
bytesRead DWORD ?
.code
main PROC
;Pass the pointer to the text string in EDX,
MOV EDX, OFFSET myText
;the array size to ECX
MOV ECX, SIZEOF myText
;pointer to the key array in ESI,
MOV ESI, OFFSET key
;the direction value (0 or 1) in EBX
MOV EBX, 0 ; rotate left for encryption
call WriteString
call Crlf
call encDecText
call WriteString
call Crlf
MOV EBX, 1
call encDecText
call WriteString
call Crlf
;bonus - get string from console and encrypt
mWriteln "Write a text to be encrypted."
; Get handle to standard input
INVOKE GetStdHandle, STD_INPUT_HANDLE
mov stdInHandle,eax
; Wait for user input
INVOKE ReadConsole, stdInHandle, ADDR buffer,
BufSize, ADDR bytesRead, 0
;encrypt and output to console
MOV EDX, OFFSET buffer
MOV ECX, BufSize
MOV EBX, 0
call encDecText
call WriteString
call Crlf
;decrypt and output to console
MOV EBX, 1
call encDecText
call WriteString
INVOKE ExitProcess,0 ; end the program
main ENDP
encDecText PROC
;Receives EDX - OFFSET of the text
; ECX - SIZE of the text
; ESI - OFFSET of the key
; EBX - rotation direction 0 for left 1 for right
PUSHAD
CMP EBX, 0
JE equals
MOV EBX, ESI
ADD EBX, 9 ;the length of key
loopNotEquals:
MOV AL, [EDX] ; value of the text
PUSH ECX
MOV CL, [ESI] ; value of the key
ROR AL, CL ; ror the text by the key
MOV [EDX], AL
POP ECX
CMP ESI, EBX ; if all the keys are used, reset the offset so it uses the beginning
JE reset1
INC ESI
JMP endReset1
reset1:
SUB ESI, 9
endReset1:
INC EDX
loop loopNotEquals
mWriteln "Input decrypted."
JMP endCMP
equals:
MOV EBX, ESI
ADD EBX, 9 ; the length of key
loopEquals:
MOV AL, [EDX] ; value of the text
PUSH ECX
MOV CL, [ESI] ; value of the key
ROL AL, CL ; rol the text by the key
MOV [EDX], AL
POP ECX
CMP ESI, EBX ; if all the keys are used, reset the offset so it uses the beginning
JE reset2
INC ESI
JMP endReset2
reset2:
SUB ESI, 9
endReset2:
INC EDX
loop loopEquals
mWriteln "Input encrypted."
endCMP:
POPAD
RET
encDecText ENDP
END main
2) Assembly language code for Greatest Common Divisor(GCD)
Include Irvine32.inc
.data
array SDWORD -1,-2,17,25,10,6,538,227,12,-36
str1 byte "Greatest common divisor is: ",0
.code
main PROC
mov ecx, LENGTHOF array /2
mov esi,OFFSET array
l1:
mov edi,2
l3:
mov eax,[esi]
add esi,4
cmp eax,0
jl l9
jmp l2
l9:
neg eax
l2:
push eax
dec edi
cmp edi,0
jne l3
call gcd
mov edx, offset str1
call writestring
call writedec
call crlf
loop l1
exit
main ENDP
gcd proc
pop edi
pop eax
pop ebx
gcd1:
mov edx,0
div ebx
mov eax,ebx
mov ebx,edx
cmp ebx,0
jg gcd1
push edi
ret
gcd endp
END main
Save the files with .asm extension.
Hope this helps. Thank you.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.