Question

Complete the following Intel assembly language program which determines whether the byte sized operand stored in...

Complete the following Intel assembly language program

which determines whether the byte sized operand stored

in memory location 'number' is prime or not. The program

will write the value of 0 into the memory location 'answer'

if the number is not prime, otherwise the initial value of '1' will

be left unmodified (indicating that the number is prime). The program

makes use of the DIV instruction to determine the value of quotient

and remainder when dividing the number by 2,3,4,... (number -1) .

section .data

; put your data in this section using

; db , dw, dd directions

number db 5

answer db 1 ; 1 means number is prime, 0 means number is not prime

section .bss

; put UNINITIALIZED data here using

section .text

global _start

_start:

mov esi, number ; get the offset of number into esi  

keith: mov eax, 0 ; clear the entire eax register

mov al, [esi] ; get the number from memory into al

mov dl, al ; put it inside dl as well

mov bl, 2 ; bl holds each divisor starting from 2

loopy: div bl ; ax / bl with quot in al and rem in ah

and ax, 1111111100000000b ; isolate the rem in ah with a AND mask

; to determine whether the remainder is 0

; YOU COMPLETE THE REST OF THE CODE

HINT: The program is to implement the following high-level

pseudocode:

prime = true ;

divisor = 2 ;

while ( divisor < number )

{

if ( the remainder of number / divisor is 0 )

{

prime = false ; // we found a divisor which evenly divides number

} // so therefore number cannot be prime

   

divisor = divisor + 1 ; // check the next divisor and keep looping

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

.... jz non_prime
.... inc bl
.... cmp bl, dl
.... jl loopy
[add code here to report a prime number]
non_prime:
[add code here to report a non-prime number]


Both the assembly code and the C pseudocode are pretty n00b-like.

1. There is no need to clear EAX if you are going to be doing all 8 and 16-bit arithmetic.

2. There's no point in AND AX, 0FF00h, when AND AH,AH does the same thing with a shorter instruction

3. There is no point in the pseudocode staying in the loop when a number is found to be non-prime. Assembly language is all goto-based, so you don't have to pretend that goto is evil and "avoid a break; statement".

4.One leading test for divisibility by 2 is the only even divisor you need to test. If a number isn't divisible by 2, it isn't going to have any other even divisors, either.

5. You can really take advantage of the fact that the DIV instruction gives you a quotient as well as a remainder. If the quotient is (strictly) less than the divisor, that means the divisor is greater than the square root of the original number. If you haven't seen a divisor yet, you aren't going to. You can stop after sqrt(n) tries and it doesn't cost a single machine cycle or a register to compare against.

Those last two This cuts the complexity for testing a prime number p down from p-2 divisions to approximately sqrt(p)/2 divisions. So, to find that 241 is prime will take 8 divisions instead of 239. I don't count division by 2 because that can be done simply by testing bit 0 of the number.

if you find my answer correct please give this a tumbs up, or we can discuss more about the answer.Thank You

Add a comment
Know the answer?
Add Answer to:
Complete the following Intel assembly language program which determines whether the byte sized operand stored in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Complete the following Intel assembly language program which determines whether the byte sized operand stored in...

    Complete the following Intel assembly language program which determines whether the byte sized operand stored in memory location 'number' is prime or not. The program will write the value of 0 into the memory location 'answer' if the number is not prime, otherwise the initial value of '1' will be left unmodified (indicating that the number is prime). The program makes use of the DIV instruction to determine the value of quotient and remainder when dividing the number by 2,3,4,......

  • Assembly Language NASM create a substring ASSIGNMENT INSTRUCTIONS: Create the Substring from the Given string, beginIndex...

    Assembly Language NASM create a substring ASSIGNMENT INSTRUCTIONS: Create the Substring from the Given string, beginIndex and endIndex The program should create a new string that is a substring of this string. The substring begins at the specified beginIndex and extends to the character at index endIndex – 1. Thus the length of the substring is endIndex-beginIndex. In other words you can say that beginIndex is inclusive and endIndex is exclusive while getting the substring. Initialize the following values in...

  • Analyze this assembly program (emu 8086) line by line! and explain the purpose of the program!...

    Analyze this assembly program (emu 8086) line by line! and explain the purpose of the program! PROGRAM: .model small .stack 100h .data    num1 db ?    num2 db ?    num3 db ?    MSG1 DB 10,13,"ENTER FIRST NUMBER : $"    MSG2 DB 10,13,"ENTER SECOND NUMBER: $"      MSG3 DB 10,13,"ENTER SECOND NUMBER: $"      MSG4 DB 10,13,"SMALLER NUMBER IS : $"    avg db ? ends .code .start    main proc        mov ax,data       ...

  • Assembly Please answer the following above, compile and single step through the program, writing...

    Assembly Please answer the following above, compile and single step through the program, writing down the value of the destination for each instruction. 1. Using Visual Studio on the system, create a project using the code given below unsigned char short int int gArray 0x09, 0xFA, 0x5A, 0x18, 0x48, 0xAC, 0xD4, 0x71 ; cAr rays 1 [ ] = { 0:09, 0xfa, Ox5A, Ox18, 0x48, OxAC, OxD4, 0x71 }; gArray! [ ] = { Ox09, OxFA, Ox5A, Ox18, 0x48, OxAC,...

  • X86 Assembly Language Help to implement the CipherChar Procedure at the end of the given code...

    X86 Assembly Language Help to implement the CipherChar Procedure at the end of the given code INCLUDE Irvine32.inc         .data       KeyPrompt BYTE "Enter the passphrase: ",0       TextPrompt BYTE "Enter the plaintest: ",0           str1 BYTE "The passphrase has length:",0           str2 BYTE "The plaintest has length:",0       KeyIs BYTE "The passphrase: ",0       PlainTextIs BYTE "The plaintext: ",0       CipherTextIs BYTE "The ciphertext: ",0       KMAX = 64                        ; passphrase buffer maximum size       BMAX = 128                       ; test...

  • Need help on Assembly language 1.Solve the following conditions: A. Suppose AL contains 11001011 and CF...

    Need help on Assembly language 1.Solve the following conditions: A. Suppose AL contains 11001011 and CF = 1.    Give the new contents of AL after each of the following instructions is executed.    Assume the above initial conditions for each part of this question. a. SHL AL,1 b. SHR AL,1 c. ROL AL,2 d. ROR AL,3 e. SAR AL,2 f. RCL AL,1 g. RCR AL,3 B. Suppose EAX contain ABCDH.    Show the contents of BX and CX after...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT