;calculam suma 1+2+...+n printr-o procedura
;n este transmis pe stiva
;folosim o variabila locala pentru index
;returnam suma in rax
includelib libcmt.lib
includelib legacy_stdio_definitions.lib
printf proto
scanf proto
.DATA	
n SQWORD 7
fmt BYTE "sum=%lld",0

.CODE
main	PROC
;sum are un singur argument transmis pe stiva
push n    ;plasam n pe stiva
call sum  ;apelam functia
add rsp,8 ;curatam stiva
;afisam suma
sub rsp,40
lea rcx,fmt
mov rdx,rax
call printf
add rsp,40
ret
main    ENDP
;long long sum(long long n)
;n=[rsp+8]
sum     PROC
sub rsp,8;alocam pe stiva spatiu pentru contorul sumei
mov rcx,[rsp+16]; rcx = [rsp+16] = n
mov rax,0;rax contine valoarea curenta a sumei
mov QWORD PTR [rsp],0; contor=0
;do { rax+=[rsp]; [rsp]++;}while(rcx<=[rsp+16])
tag:
    add rax,[rsp]
    inc qword ptr [rsp]
    cmp [rsp],rcx
    jle tag
add rsp,8
ret
sum     ENDP
END
