Translate the following C program to Pep/9 assembly language. #include #include
ID: 3804196 • Letter: T
Question
Translate the following C program to Pep/9 assembly language.
#include
#include const int limit = 5;
int main() {
int number;
scanf("%d", &number);
while (number < limit) {
number++;
printf("%d ", number);
}
return 0;
}
this is what I got so far and was wondering if it is correct
BR main
limit: .BLOCK 5 ;constant
number: .EQUATE 0 ;local variable #2d
;
main: SUBSP 2,i ;push #number
DECI number,s ;scanf("%d", &number)
while: LDWA number,s ;if (number < limit)
CPBA limit, i
BRGE endWh
ADDA 1,s
NEGA
STWA number,s
endWh: DECO number,s ;printf("%d", number)
ADDSP 2,i
STOP
.END
Explanation / Answer
main:
push rbp ; base pointer points to base of stack
mov rbp, rsp ; copy stack pointer to base pointer
sub rsp, 16 ;allocate space for 1-15 bytes of local variable
lea rax, [rbp-4] ; load effictive address
mov rsi, rax ; copy rax value into rsi
mov edi, OFFSET FLAT:.LC0
mov eax, 0
call scanf ; scanf("%d", &number);
.L3: ; while loop
mov eax, DWORD PTR [rbp-4]
cmp eax, 4
jg .L2
mov eax, DWORD PTR [rbp-4]
add eax, 1 ; num++
mov DWORD PTR [rbp-4], eax
mov eax, DWORD PTR [rbp-4]
mov esi, eax
mov edi, OFFSET FLAT:.LC1
mov eax, 0
call printf ; printf("%d", number);
jmp .L3
.L2:
mov eax, 0
leave
ret ; return
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.