In: Computer Science
Assembly Question: I am trying to annotate this code and am struggling to understand what it is doing. Can someone please add comments?
.data
.star: .string "*"
.line: .string "\n"
.input: .string "%d"
.count: .space 8
.text
.global main
printstars:
push %rsi
push %rdi
_printstars:
push %rdi
mov $.star, %rdi
xor %rax, %rax
call printf
pop %rdi
dec %rdi
cmp $0, %rdi
jg _printstars
mov $.line, %rdi
xor %rax, %rax
call printf
pop %rdi
pop %rsi
ret
printstarpyramid:
mov %rdi, %rsi
mov $1, %rdi
_printstarpyramid:
call printstars
inc %rdi
cmp %rsi, %rdi
jle _printstarpyramid
ret
main:
mov $.input, %rdi
mov $.count, %rsi
xor %rax, %rax
call scanf
mov (.count), %rdi
call printstarpyramid
//declare variable
.data
.star: .string "*"
//Read input
.line: .string "\n"
.input: .string "%d"
//Count space
.count: .space 8
.text
//Declare global variable
.global main
// Push values into variable s
printstars:
push %rsi
push %rdi
_printstars:
push %rdi
// Move values
mov $.star, %rdi
// Do xor operation
xor %rax, %rax
//Print value
call printf
pop %rdi
dec %rdi
// Compare values
cmp $0, %rdi
jg _printstars
// Move values from one to other
mov $.line, %rdi
// Xor operation
xor %rax, %rax
//Print value
call printf
// Get values
pop %rdi
pop %rsi
ret
printstarpyramid:
//Move values
mov %rdi, %rsi
mov $1, %rdi
_printstarpyramid:
call printstars
inc %rdi
cmp %rsi, %rdi
jle _printstarpyramid
ret
main:
mov $.input, %rdi
mov $.count, %rsi
xor %rax, %rax
call scanf
mov (.count), %rdi
call printstarpyramid
Instruction Working
MOV -- copies the data item referred to by its second operand
PUSH-- places its operand onto the top of the hardware supported stack in memory
POP -- removes the 4-byte data element from the top of the hardware-supported stack into the specified operand (i.e. register or memory location)
INC -- increments the contents of its operand by one
CMP -- Compare the values of the two specified operands
Syntax
cmp <reg>,<reg>
cmp <reg>,<mem>
cmp <mem>,<reg>
cmp <reg>,<con>
JLE - one of the conditional jump instruction
Sytax - jle <label> (jump when less than or equal to)