Author Topic: Help with 64 bit stack  (Read 4026 times)

Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Help with 64 bit stack
« on: October 18, 2014, 05:13:18 AM »
Hi. This is my first post. I took an example to display register from the internet and changed it to 64-bit (linux). But the code displays garbage. This prog use 16-byte char array inside the function and use it for stosb instruction to display register string. Can u help me point out where I did wrong?
Code: [Select]
mov r10,0bcfefab4323c5677h
push r10
call display_register

xor rdi,rdi ;exit
mov rax,60
syscall

display_register:
push rbp          
    mov rbp,rsp
    sub rsp,16   ;local space for string
    cld        
    lea rdi,[rbp-16]     ;address of string for stosb
    mov r10,[rbp+16] ;first argument
    xor rax,rax
    mov rcx,16
start:
shld rax,r10,4
add al,30h
cmp al,39h
jbe normal
add al,7
normal:
stosb
dec rcx
test rcx,rcx
jz disp_string
rol r10,4
jmp start
disp_string:
mov edx,16  ;size
mov edi,1   ;stdout
mov eax,1  ;sys_write   
lea rsi,[rbp-16]  ;address of string
syscall
add rsp,16
mov rsp,rbp       
    pop rbp
    ret

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2601
  • Country: us
Re: Help with 64 bit stack
« Reply #1 on: October 18, 2014, 09:39:43 AM »
Hi stressful,

I can't test 64-bit code right now, but let me take a guess. Does the first character display correctly?  When we do this in 16- or 32-bit code, we need to mask off the one nibble we want. I suspect that, after the first time through the loop, you're getting two nibbles in al, which would print garbage. Try
Code: [Select]
and rax, 0Fh
just after the "shld", or it might be better to move the "xor rax, rax" down inside the loop - just before the "shld". See if either of those ideas work...

In order to use a more consistant calling convention, you might want to pass the value in rdi instead of on the stack. If you pass it on the stack, you might want to "clean up" the stack after your function returns. Also, the "dec" ought to set the zero-flag, even without the "test". I don't think those are a problem.

Best,
Frank


Offline stressful

  • Full Member
  • **
  • Posts: 105
  • Country: 00
    • CPU2.0
Re: Help with 64 bit stack
« Reply #2 on: October 18, 2014, 10:47:31 AM »
You guessed it correct. Now it's working as expected. 
I thought it was something structural in 64-bit; made me missed the other minor details. Thanks for the help.