Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C code example: #include #include unsigned int read_integer(void) { unsigned int n; scanf(%u, & n); return n; } void print_prime(void) { printf(prime ); } void

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

C code example:

#include #include unsigned int read_integer(void) { unsigned int n; scanf("%u", & n); return n; } void print_prime(void) { printf("prime "); } void print_not_prime(void) { printf("not prime "); } int main(void) { unsigned int i; unsigned int n = read_integer(); bool is_prime; while (n != 0) { if (n == 1) { print_not_prime(); } else { is_prime = true; for (i = 2; i * i

Assembly skeleton to be used:

; skeleton.asm ; my required comments

USE32 ; tell nasm to assemble 32 bit code

global _start ; export the start symbol for our program _start: ; tell the linker where our program begins ; beginning of program ; TODO my code here

; exit mov ebx, 0 mov eax, 1 int 80h

; functions here

print_not_prime:

; TODO

print_prime: ; set up stack frame push ebp ; save the current stack frame mov ebp, esp ; set up a new stack frame ; save modified registers push eax ; save eax push ebx ; save ebx push ecx ; save ecx push edx ; save edx

; write prime to stdout mov eax, 4 ; syscall 4 (write) mov ebx, 1 ; file descriptor (stdout) mov ecx, .prime ; pointer to data to load mov edx, .prime.l ; byte count int 0x80 ; issue system call

; cleanup pop edx ; restore edx pop ecx ; restore ecx pop ebx ; restore ebx pop eax ; restore eax pop ebp ; restore ebp ret ; return to caller

.prime: db "prime", 10 .prime.l equ $-.prime

read_integer: ; set up stack frame push ebp ; save the current stack frame mov ebp, esp ; set up a new stack frame ; set up local variables sub esp, 8 ; allocate space for two local ints mov dword [ebp-4], '0' ; digit: initialize to '0' mov dword [ebp-8], 0 ; value: initialize to 0

; save modified registers push ebx ; save ebx push ecx ; save ecx push edx ; save edx

.read_loop: ; update number calculation mov eax, 10 ; load multiplier mul dword [ebp-8] ; multiply current value by 10, store in eax add eax, [ebp-4] ; add new digit to current value sub eax, '0' ; convert digit character to numerical equivalent mov [ebp-8], eax ; save new value ; read in digit from user mov eax, 3 ; syscall 3 (read) mov ebx, 0 ; file descriptor (stdin) lea ecx, [ebp-4] ; pointer to data to save to mov edx, 1 ; byte count int 0x80 ; issue system call

; loop until enter is pressed cmp dword [ebp-4], 10 ; check if end of line reached jne .read_loop ; if not, continue reading digits

; cleanup mov eax, [ebp-8] ; save final value in eax pop edx ; restore edx pop ecx ; restore ecx pop ebx ; restore ebx add esp, 8 ; free local variables pop ebp ; restore ebp ret ; return to caller

Lab 6 Primality, Revisited CSE 2421 Systems I Reminder: All labs are to be completed entirely on your own. Information about the Linux environment and your CSE account can be found as separate references. Refer to the General Lab Guidelines for details on grading, submission, and academic misconduct. Demonstrating Assembly basics Assembly logic flows Assembly registers Assembly vs. C Overview In this lab you will implement an assembly program to determine whether or not a series of numbers are prime. A positive integer is said to be prime if it has no positive divisors other than 1 and itself. The number 1 is not considered prime. As an example, the first 10 prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 Outline The user will enter a non-negative integer. If the integer is 0, your program exits. If the integer is non-zero, your program determines if it is a prime number or not o If the number is prime, the program prints "prime" o If the number is not prime, the program prints "not prime" The program continues until the user enters 0 Design A simple C version of this program is provided in the appendix to this lab. o Your assembly code should recreate the C version of this lab. For example, if you needed to convert a C "for" loop to assembly, implementing the same functionality as a switch statement would not be appropriate. If the C code says i i

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management Systems Designing And Building Business Applications

Authors: Gerald V. Post

1st Edition

0072898933, 978-0072898934

More Books

Students also viewed these Databases questions

Question

=+Where does the focus of labor relations lie? Is it collective

Answered: 1 week ago