Stack operation and memory align in MASM X64

14

I have an application that takes a TAC (IL) code and generates an ASM code using x64 MASM. The problem is that I'm not having any compile errors, and yes (at least that's what I think) when building the stack . The program below is an MMC, so if I enter 3 and 5 as input, it should return me 15, but it is returning random numbers like 1281237 and 230932811.

I know that in x86, the alignment works in 4 bytes, and I read in some places that in x64 should be 16 bytes. But if I put 16, I get a memory error when I run the same, so I'm lining up in 8 bytes, but this may be wrong, I'm not sure, there's almost no MASM64 documentation available out there.

extern ExitProcess:proc
extern printf:proc
extern scanf:proc

includelib kernel32.lib
includelib user32.lib
includelib msvcrt.lib
include invoke_macros.asm

.data
scan BYTE 'scanf:',0
formatInt BYTE '%d',0
msg BYTE 'Return = %d',0
printInt BYTE 'printf: %d', 0ah, 0h
f1  BYTE 'Fake parameter #1 ( 137 - 279 ):',0

.data?
din dq ?

.code

start PROC
invoke  printf, addr f1
invoke  scanf, addr formatInt, addr din
MOV rax, din
PUSH rax
CALL sub_411420
ADD rsp, 8
invoke printf, addr msg, rax
RET
start ENDP

sub_411B00 proc
PUSH rbp
MOV rbp, rsp
SUB rsp, 48
MOV rax, [rbp + 16]
MOV [rbp - 24], rax
MOV rax, [rbp + 12]
MOV [rbp - 16], rax
LABEL_1:
MOV rax, [rbp - 24]
MOV rbx, [rbp - 16]
CDQ
DIV rbx
MOV rax, rdx
MOV [rbp - 8], rax
MOV rax, [rbp - 16]
MOV [rbp - 24], rax
MOV rax, [rbp - 8]
MOV [rbp - 16], rax
MOV rax, [rbp - 8]
CMP rax, 0
JG LABEL_1
MOV rax, [rbp + 12]
MOV rbx, [rbp + 16]
MUL rbx
MOV rbx, [rbp - 24]
CDQ
DIV rbx
MOV [rbp - 48], rax
JMP LABEL_4
LABEL_4:
MOV eax, [rbp - 48]
ADD rsp, 48
POP rbp
RET
sub_411B00 endp

sub_411420 proc
PUSH rbp
MOV rbp, rsp
SUB rsp, 48
PUSH [rbp - 24]
invoke  printf, addr scan
invoke  scanf, addr formatInt, addr din
MOV rax, din
MOV [rbp - 24], rax
PUSH [rbp - 16]
invoke  printf, addr scan
invoke  scanf, addr formatInt, addr din
MOV rax, din
MOV [rbp - 16], rax
PUSH [rbp - 24]
PUSH [rbp - 16]
PUSH [rbp + 8]
CALL sub_411B00
MOV [rbp - 8], rax
PUSH [rbp - 8]
POP rax
invoke  printf, addr  printInt, rax
PUSH rax
MOV eax, [rbp - 8]
ADD rsp, 96
POP rbp
RET
sub_411420 endp

end

So that's my question. How does the stack work and the memory align in x64? Thanks!

    
asked by anonymous 25.05.2015 / 13:01

0 answers