In: Computer Science
Write an assembly language program that repeatedly prompts the
user to enter signed decimal integer numbers. The program should be
run from the command prompt, output a text prompt to the screen,
and then wait for the user to type in a number followed by the
Enter key. (The legitimate range of user input values is any signed
integer that can be represented in 32 bits.) After each number is
entered, the program should determine and display the following
information about the number: whether it is positive or negative;
whether or not it is evenly divisible by 16; and whether or not it
can be represented in 16 bits (in other words, is it in the range
-32768 x +32767). For example, if the user entered the number +5,
the output generated by the program should look similar to
this:
+5 is a positive number.
+5 is not evenly divisible by 16.
+5 can be represented in 16 bits.
On the other hand, if the user entered -32816, the output would
look like this:
-32816 is a negative number.
-32816 is evenly divisible by 16.
-32816 cannot be represented in 16 bits.
After determining and displaying the above information, the program
should prompt the user to enter another number. This process should
continue until the user enters the value 0 (which is neither
positive nor negative). At that point, execution should terminate
and return to the command prompt.
Assemble, link, and test your program. Make sure to test it for
each of the eight possible input cases (permutations of positive
vs. negative, divisible by 16 vs. not divisible by 16, and fits in
16 bits vs. does not fit in 16 bits), as well as the ninth
possibility (the special case of 0, which exits the program). When
you are sure it is working, run it from the command prompt and
capture a screen shot(s) of a test run that illustrates all nine
possibilities and the corresponding outputs.
Required Assembly code is given below:
.LC0:
.string "%d"
.LC1:
.string "%d is a positive number.\n"
.LC2:
.string "%d is a negative number.\n"
.LC3:
.string "%d is evenly divisible by 16.\n"
.LC4:
.string "%d is not evenly divisible by 16.\n"
.LC5:
.string "%d can be represented in 16 bits.\n"
.LC6:
.string "%d cannot be represented in 16 bits.\n"
main:
push rbp
mov rbp, rsp
sub rsp, 16
.L8:
lea rax, [rbp-4]
mov rsi, rax
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call __isoc99_scanf
mov eax, DWORD PTR [rbp-4]
test eax, eax
jle .L2
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
.L2:
mov eax, DWORD PTR [rbp-4]
test eax, eax
jns .L3
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC2
mov eax, 0
call printf
.L3:
mov eax, DWORD PTR [rbp-4]
and eax, 15
test eax, eax
jne .L4
mov eax, DWORD PTR [rbp-4]
test eax, eax
je .L4
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC3
mov eax, 0
call printf
.L4:
mov eax, DWORD PTR [rbp-4]
and eax, 15
test eax, eax
je .L5
mov eax, DWORD PTR [rbp-4]
test eax, eax
je .L5
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC4
mov eax, 0
call printf
.L5:
mov eax, DWORD PTR [rbp-4]
cmp eax, -32768
jl .L6
mov eax, DWORD PTR [rbp-4]
cmp eax, 32767
jg .L6
mov eax, DWORD PTR [rbp-4]
test eax, eax
je .L6
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC5
mov eax, 0
call printf
.L6:
mov eax, DWORD PTR [rbp-4]
cmp eax, -32768
jge .L7
mov eax, DWORD PTR [rbp-4]
cmp eax, 32767
jle .L7
mov eax, DWORD PTR [rbp-4]
test eax, eax
je .L7
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC6
mov eax, 0
call printf
.L7:
mov eax, DWORD PTR [rbp-4]
test eax, eax
jne .L8
mov eax, 0
leave
ret
Screenshot of above code output is given below:
PLEASE UPVOTE THE ANSWER AND FEEL FREE TO ASK YOUR DOUBTS IN COMMENT SECTION