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

WHILE B > 0 A = A mod B swap A and B ENDWHILE GCD = A write an assembly language

ID: 3854752 • Letter: W

Question

WHILE B > 0

A = A mod B

swap A and B

ENDWHILE

GCD = A

write an assembly language program (Kip Irvine.32) which finds pythagorean triples using the above algorithm. There is no input to your program. Your output should be a list of the triples displayed on the screen, one triple per line.

Implement the TRIPLE and GCD routines as procedures called TRIPLE and GCD. use registers EAX and EBX to pass U and V to the procedures, and pass the gcd back in register EAX. The procedures should protect are registers used.

Note: The procedures TRIPLE and GCD should NOT reference the values U and V stored in the main procedure. That is, sue registers to pass parameters.

Explanation / Answer

GCD PROCEDURE

gcd PROC A:DWORD , B:DWORD

mov eax,myU

mov A,eax

mov ebx , myV

mov B, ebx

while 1: mov ebx,B

cmp ebx , 0

jna endwh1

mov eax , A

mov edx, 0

div B

xchg edx,B

mov A, edx

jmp while1

endwh1: mov eax,A

ret

exit

gcd ENDP

INCLUDE Irvine32.inc

.data

myMessage BYTE "The list of all possible Pythagorean Triples:" ,0dh,0ah,0

myX BYTE "x:",0

myY BYTE "y:",0

myZ BYTE "z:",0

myU DWORD ?

myV DWORD ?

mytempX DWORD ?

mytempY DWORD ?

mytempZ DWORD ?

.code

main PROC

mov edx , OFFSET myMessage

call WriteString

mov myU , 2

dowh1: mov eax, myU

and eax , 1

jnz else1

mov myV , 1

jmp dowh2

else1: mov myV ,2

dowh2: call gcd

cmp eax,1

jne else2

mov eax , myU

mov ebx, myV

call triple

jmp enddo2

else2: add myV , 2

mov eax , myV

cmp eax , myU

jb dowh2

enddo2: add myU , 1

mov eax , myU

cmp eax , 10

jb dowh1

exit

main ENDP

TRIPLE PROCEDURE

mov eax,myU

mov U,eax

mov ebx,myV

mov V , ebx

mov eax , U

mul U

mov mytempX , eax

mov eax , V

mul V

sub mytempX , eax

mov eax , U

mov ecx , V

mul ecx

add eax , eax

mov mytempY , eax

mov eax , U

mul U

mov mytempZ , eax

mov eax , V

mul V

add mytempZ , eax

mov eax , mytempX

cmp eax , mytempY

jna ENDIF1

mov eax , mytempX

xchg eax , mytempY

mov mytempX , eax

ENDIF1: mov edx , OFFSET myX

call WriteString

mov eax , mytempX

call WriteInt

mov edx , OFFSET myY

call WriteString

mov eax , mytempY

call WriteInt

mov edx , OFFSET myZ

call WriteString

mov eax , mytempZ

call WriteInt

call CRLF

ret

exit

triple ENDP

END main