In: Computer Science
Write an assembly language program which either hardcodes or reads in the lengths of the sides of a triangle, and outputs a truthy value (1, “true”, “yes”, and others) indicating whether the triangle is a valid one or not (0, “false”, “no”, etc). A triangle is valid if the sum of each pair of sides is greater than the unpaired side. Meaning if a, b, c are the three sides of a triangle, then the triangle is valid if all three of these conditions are satisfied:
• a + b > c
• a + c > b
• b + c > a
This would invalidate a triangle such as a=3, b=3, c=6, which would only be a line. Use this as a test case, as well as creating your own test cases.
I found a similar question that was answered but I wanted to see if someone else had different insight into this problem. I am still trying to learn assembly.
The Software Used for Writing ALP is MASM-32
Procedure:
1.) Save the program using .asm extension written in q-editor of masm-32 software
2.) Open the command prompt from Q-editor and type the following
set path=c:/masm32/bin
ml/Zi filename.asm
cv filename.exe
3.) Observe the registers by clicking F5 (direct result) or F10(step by step execution)
Program:
.model small
.dosseg
.data
A dw 01H
B dw 02H
C dw 03H
msg1 db 'Valid Triangle','$'
msg2 db 'Not a Valid Triangle','$'
.code
START:
MOV AX, @DATA;
MOV DS, AX; // intialising data segment with data
XOR AX, AX; // clearing accumulator register
XOR BX, BX; // clearing base register
XOR CX, CX; // clearing count register
XOR DX, DX; // clearing data register
MOV AX, A; // move A data into Accumulator register
MOV BX, B; // move B data into Base register
MOV CX, C; // move C data into Count register
ADD AX, BX; // Adding contents of ax,bx and storing in ax
ADD BX, CX; // Adding contents of bx,cx and storing in bx
ADD CX, AX; // Adding contents of cx,ax and storing in cx
CMP AX, C; // comparing content of ax with C (if C is bigger carry occurs)
JNC L1; // if content of ax is greater than C , jump to L1
MOV DX, OFFSET msg2; // moving address of msg2 into dx
MOV AH,09H; // moving 09h to ah
INT 21H; //sending an interrupt to stop execution
HLT; // stop execution
L1: CMP BX, A; // comparing bx with A
JNC L2; // if no carry occurs jump to L2
MOV DX, OFFSET msg2; // move address of msg2 to dx
MOV AH,09H; //move 09h to ax
INT 21H; //interrupt the program
HLT; //stop execution
L2: CMP CX, B; // comparing cx and B
JNC L3; // jump if no carry to L3
MOV DX, OFFSET msg2; // moving address of msg2 to dx
MOV AH,09H; //moving 09h to ah
INT 21H; //interrupt the program
HLT; // stop execution
L3: MOV DX, OFFSET msg1; // moving address of msg1to dx
MOV AH,09H; // moving 09h to ah
INT 21H; // interrupt program
HLT; // stop execution
END START; //end the start
END; // end of program