Author Topic: Beginner question  (Read 3910 times)

geimas5

  • Regular Member
  • *
  • Posts: 4
Beginner question
« on: October 18, 2014, 09:33:10 PM »
Hello,

I am having a problem with calling the pow procedure, and I hope someone can help me out.

The code I have a problem with is below, and it fails with an "Access violation reading location 0x0000000000000000".

Thanks,


main PROC
push rbp
mov rbp, rsp
sub rsp, 0CCh
L0:
MOVSD XMM2, real1931608064
MOVD R10, XMM2
MOV [RBP -8], R10
MOVSD XMM2, real644874237
MOVD R10, XMM2
MOV [RBP -16], R10
MOV R8, [RBP -8]
MOVD XMM0, R8
MOV R8, [RBP -16]
MOVD XMM1, R8
CALL pow
MOVD R10, XMM0
MOV [RBP -24], R10
MOV RCX, [RBP -24]
CALL PrintDouble
L5:
MOV R10, 0
MOV [RBP -32], R10
MOV RAX, [RBP -32]
JMP mainexit
mainexit:
call exit
mov rsp, rbp
pop rbp
ret
main ENDP

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Beginner question
« Reply #1 on: October 18, 2014, 10:59:33 PM »
Hi geimas5,

could you please attach the entire code as a ZIP file? So we can see where's the problem. For now: Please remember the necessary shadow space and the 16-bit aligned stack for the function calls. You can find appropriate examples here. Please use the archive Win64U1.zip. It contains examples for MASM/JWasm and NASM/YAS. I hope that helps. And welcome to the forum.

Gunther
Get your facts first, and then you can distort them.

geimas5

  • Regular Member
  • *
  • Posts: 4
Re: Beginner question
« Reply #2 on: October 19, 2014, 04:40:06 AM »
Thank you!

With that example I was able to correct my code.

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Beginner question
« Reply #3 on: October 19, 2014, 10:30:02 PM »
Thank you!

With that example I was able to correct my code.

You're welome. Don't hesitate to post further questions.

Gunther
Get your facts first, and then you can distort them.

geimas5

  • Regular Member
  • *
  • Posts: 4
Re: Beginner question
« Reply #4 on: October 28, 2014, 10:30:38 PM »
Looks like I was a little quick in assuming I had fixed the problem.

I have attached the file that fails. I think I might have misunderstood what 16-bit alignment means.

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Beginner question
« Reply #5 on: October 29, 2014, 12:26:49 AM »
Hi geimas5,

I've checked your source code. It is not very structured and hard to read. I think you should load it into an editor for a better formatting. Furthermore, the 16-bit alignment must be safe for every function call:
Code: [Select]
; print_Cstring
; Purpose:         Print a C string to STDOUT via libc.
; Input:           rdx -> string address
; Output:          String on STDOUT.
print_Cstring proc
        lea        rcx, str_format       ; rcx -> format string
        sub        rsp, 40
        call       printf                ; call libc
        add        rsp, 40
        ret
print_Cstring endp

48 is divisible by 16. So you have to sub 48 from the current RSP. But the CALL needs 8 byte for themselves. Therefore the subtraction of 40, after that the function call and after that the addition of 40. That's the way to go. I hope that helps.

Gunther
Get your facts first, and then you can distort them.

geimas5

  • Regular Member
  • *
  • Posts: 4
Re: Beginner question
« Reply #6 on: October 29, 2014, 02:28:28 AM »
Thank you Gunther. I managed to get it working again now.

I am sorry about the bad structure of the code. I am currently trying to make a compiler and this is the output from the compiler for one of my test cases. But I will clean it up before posting if I get stuck again.

Gunther

  • Member
  • *****
  • Posts: 3585
  • Forgive your enemies, but never forget their names
Re: Beginner question
« Reply #7 on: October 29, 2014, 05:09:28 AM »
Thank you Gunther. I managed to get it working again now.

I am sorry about the bad structure of the code. I am currently trying to make a compiler and this is the output from the compiler for one of my test cases. But I will clean it up before posting if I get stuck again.

Go forward.  :t

Gunther
Get your facts first, and then you can distort them.