Create an HLA Assembly language program that prompts for a value and then prints
ID: 3913107 • Letter: C
Question
Create an HLA Assembly language program that prompts for a value and then prints a particular number series based on that entered number. You should print out each number separated by an underscore. Each number should alternate, positive or negative from the prior number.
Here are some example program dialogues to guide your efforts:
Gimme data: 12
12_13_11_14_10_15_9_16_8_17_7_18
Gimme data: 3
3_4_2
In an effort to help you focus on building an Assembly program, I’d like to offer you the following C statements which match the program specifications stated above. If you like, use them as the basis for building your Assembly program.
SAMPLE C CODE:
------------------------
int start;
int amount = 1; /* the delta amount to add to each value as the code loops */
bool positive = true; /* are we adding or subtracting from the delta */
printf( "Gimme data: " );
scanf( "%d", &start );
int value = start;
for (int i = 1; i <= start; i++)
{
if (i != 1)
{
printf( "_" );
}
printf( "%d", &value );
if (positive)
{
value = value + amount;
}
else
{
value = value - amount;
}
amount = amount + 1;
positive = !positive;
}
printf( " " );
Explanation / Answer
X86-64 GCC 8.1 Code:
.LC0:
.string "Gimme data: "
.LC1:
.string "%d"
main:
push rbp
mov rbp, rsp
sub rsp, 32
mov DWORD PTR [rbp-4], 1
mov BYTE PTR [rbp-5], 1
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call printf
lea rax, [rbp-16]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call __isoc99_scanf
mov eax, DWORD PTR [rbp-16]
mov DWORD PTR [rbp-20], eax
mov DWORD PTR [rbp-12], 1
jmp .L2
.L6:
cmp DWORD PTR [rbp-12], 1
je .L3
mov edi, 95
call putchar
.L3:
lea rax, [rbp-20]
mov rsi, rax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf
cmp BYTE PTR [rbp-5], 0
je .L4
mov edx, DWORD PTR [rbp-20]
mov eax, DWORD PTR [rbp-4]
add eax, edx
mov DWORD PTR [rbp-20], eax
jmp .L5
.L4:
mov eax, DWORD PTR [rbp-20]
sub eax, DWORD PTR [rbp-4]
mov DWORD PTR [rbp-20], eax
.L5:
add DWORD PTR [rbp-4], 1
movzx eax, BYTE PTR [rbp-5]
test eax, eax
setne al
xor eax, 1
movzx eax, al
mov BYTE PTR [rbp-5], al
and BYTE PTR [rbp-5], 1
add DWORD PTR [rbp-12], 1
.L2:
mov eax, DWORD PTR [rbp-16]
cmp DWORD PTR [rbp-12], eax
jle .L6
mov edi, 10
call putchar
mov eax, 0
leave
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.