215k views
4 votes
Write a function (Stars) that uses a variable which's you can hardcode or pass its address in ECX to print stars equal in number to that variable.

So if the variable had 12, the program should print

************

Sample main() code

mov al,230;
mov [number],al
call Stars;
jmp end
If you want to use AX you can increase the number of * you can print, here is what the main would look like:

mov ax,5000;
mov [number],ax
call Stars;
jmp end

User Ivan Salo
by
7.9k points

1 Answer

3 votes

Stars:

push ebp ; save the base pointer

mov ebp, esp ; set the base pointer to the current stack pointer

mov eax, [ebp+8] ; load the parameter into eax

print_loop:

cmp eax, 0 ; compare the value of the parameter with zero

jle end_print_loop ; jump out of the loop if the value is less than or equal to zero

push eax ; save the value of eax on the stack

mov eax, '*' ; set eax to the character code for the asterisk

push eax ; save the value of eax on the stack

call putchar ; call the putchar function to print the asterisk

add esp, 8 ; clean up the stack by adding 8 bytes

sub eax, 1 ; decrement the value of eax

jmp print_loop ; jump back to the beginning of the loop

end_print_loop:

pop ebp ; restore the base pointer

ret ; return control to the caller

To call this function from the main program and print 12 asterisks, you can use the following code:

mov ecx, 12 ; load the parameter (12) into ecx

call Stars ; call the Stars function

This will print out 12 asterisks (************) to the console.

User Josep Pueyo
by
8.8k points