Hi, i am suppose to create an assembly language program in which prints the numb
ID: 3680910 • Letter: H
Question
Hi, i am suppose to create an assembly language program in which prints the numbers 1-100. When a number is divisble by 5 it prints "fizz" and when theres a 5 in the number it also prints out "fizz" I have got the first part printing 1-100 out to work but i am not sure why my ecx counter resets after it prints fizz. any help would be appreciated
section .text
extern puts
extern printf
global main
main:
L1:
inc ecx ;ecx = 1
mov eax,ecx ;eax = 1
xor ebx,ebx
xor edx,edx
mov ecx,eax
mov ebx, 5 ;ebx = 5
idiv ebx ;eax / ebx
cmp edx,0 ; is eax = 0?
jz L2
jmp L3
L3:
push ecx ;ecx = 1 onto stack
push a ;calling string
call printf ;printing string plus ecx
pop ecx ; pops a (string)
pop ecx ; pops 1
cmp ecx,100 ; ecx = 1
jl L1 ; return to loop
jmp exit
L2:
push ebx
push eax
push b
call printf
pop eax
pop eax
pop ebx
jmp L1
exit:
ret
Explanation / Answer
The assembly language program can be wriiten as follows-
.LC0:
.string "%d"
.LC1:
.string "fizz"
main:
pushq %rbp
movq %rsp, %rbp
subq $16, %rsp
movl $1, -16(%rbp)
.L3:
movl -16(%rbp), %eax
cmpl $100, %eax
jg .L2
leaq -16(%rbp), %rax
movq %rax, %rsi
movl $.LC0, %edi
movl $0, %eax
call printf
movl -16(%rbp), %eax
addl $1, %eax
movl %eax, -16(%rbp)
jmp .L3
.L2:
movl $1, -16(%rbp)
.L6:
movl -16(%rbp), %eax
cmpl $100, %eax
jg .L4
movl -16(%rbp), %ecx
movl $1717986919, %edx
movl %ecx, %eax
imull %edx
sarl %edx
movl %ecx, %eax
sarl $31, %eax
subl %eax, %edx
movl %edx, %eax
movl %eax, %edx
sall $2, %edx
addl %eax, %edx
movl %ecx, %eax
subl %edx, %eax
testl %eax, %eax
jne .L5
movl $.LC1, %edi
movl $0, %eax
call printf
.L5:
movl -16(%rbp), %eax
addl $1, %eax
movl %eax, -16(%rbp)
jmp .L6
.L4:
movl $1, -16(%rbp)
.L11:
movl -16(%rbp), %eax
cmpl $100, %eax
jg .L7
movl -16(%rbp), %eax
movl %eax, -4(%rbp)
movl $10, -8(%rbp)
.L10:
cmpl $0, -4(%rbp)
je .L8
movl -4(%rbp), %eax
cltd
idivl -8(%rbp)
movl %eax, -12(%rbp)
movl -4(%rbp), %eax
cltd
idivl -8(%rbp)
movl %edx, -4(%rbp)
movl -8(%rbp), %ecx
movl $1717986919, %edx
movl %ecx, %eax
imull %edx
sarl $2, %edx
movl %ecx, %eax
sarl $31, %eax
subl %eax, %edx
movl %edx, %eax
movl %eax, -8(%rbp)
cmpl $5, -12(%rbp)
jne .L10
movl $.LC1, %edi
movl $0, %eax
call printf
jmp .L10
.L8:
movl -16(%rbp), %eax
addl $1, %eax
movl %eax, -16(%rbp)
jmp .L11
.L7:
movl $0, %eax
leave
ret
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.