2

I am trying to learn assembly, so the code may not be entirely correct. When running the assembly code, i expect it to print the numbers 321 132 123 on separate lines. When I run the program on git bash and windows command line, I get the expected behaviour. However, when I run it using Cmder console emulator, it only prints 321. I have not been able to find the reason why. Cmder has the following return code: -1073741819

The assembly code is below. I run it on Windows 10 and create an executable as such:

NASMLINK=/nologo /subsystem:console /defaultlib:msvcrt.lib /defaultlib:legacy_stdio_definitions.lib /defaultlib:Kernel32.lib
nasm -f win64 -o Test.obj Test.asm   
link $(NASMLINK) Test.obj /OUT:Test.exe
bits 64
default rel

segment .data
    format: db "%lld", 0xa, 0x0

segment .text
    global main
    extern printf

PrintInt:
    push    rbp
    mov     rbp, rsp
    sub     rsp, 40
    mov     rdx, rcx
    lea     rcx, [format]
    call    printf
    add     rsp, 40
    pop     rbp
    ret


Test:
    push    rbp      ;Edit1
    mov     rbp, rsp ;Edit1
    sub     rsp, 200

    mov     r8, 132
    mov     rcx, r8
    call    PrintInt

    add     rsp, 200
    pop     rbp     ;Edit1
    xor     rax, rax;Edit1
    ret


main:
    push    rbp
    mov     rbp, rsp
    sub     rsp, 200

    mov     r8, 321
    mov     rcx, r8
    call    PrintInt

    call    Test

    mov     r8, 123
    mov     rcx, r8
    call    PrintInt

    add     rsp, 200
    pop     rbp
    xor     rax, rax
    ret

Edit1 I found that by deleting the lines commented Edit1 then it will run correctly in all command lines. However, I still do not understand why the command lines behave differently when the Edit1 lines are there.

  • Windows uses CR LF which may affect buffering although it should not cause an error. Anyway, try using `0xd, 0xa`. Your stack usage seems fine from a glance, even if a little excessive. – Jester Feb 01 '22 at 13:40
  • Using `0xd, 0xa` does not change the behaviour. I know about the stack usage, but just use 200 to avoid thinking about it for now. – yonos bjørn Feb 01 '22 at 13:46
  • That exit code is `STATUS_ACCESS_VIOLATION`, so it crashed! – CherryDT Feb 01 '22 at 14:44
  • By the way, is there any reason why you use `add rsp, xxx` at the end of your functions instead of `mov rsp, rbp`? – CherryDT Feb 01 '22 at 14:46
  • Why does it crash when using Cmder and not the others command lines? Is it a bug in Cmder perhaps? I did not know `mov rsp, rbp` was an option. I will read more about it. – yonos bjørn Feb 01 '22 at 14:54
  • I can't spot the issue yet just from looking at it but it sounds as if there is a bug in your program that makes it crash under certain conditions, and it just "coincidentally" works outside of Cmder, and with Cmder around it the stars align wrongly and it crashes. It would be interesting to know where exactly it crashed. You could use x64dbg for example and then temporarily configure your tool to be automatically debugged when launched (using GFlags, configured something like this https://cherryshare.at/i/E7Vl6R/image.png). This way you can still launch it from inside Cmder but debug it. – CherryDT Feb 01 '22 at 15:00
  • 4
    Your `PrintInt` function misaligns the stack. This results in `printf` being called with a misaligned stack, which is an ABI violation, and crashes or erroneous behavior become possible. Your "fix" is to make `Test` *also* misalign the stack. The two errors cancel out, resulting in `printf` being called on an aligned stack. The correct fix is not to make two errors in a row that cancel out, but rather to make no errors at all. Fix the stack alignment in `PrintInt`. – Raymond Chen Feb 01 '22 at 15:11
  • 4
    I built the program and debugged it, and I can see the same thing. The reason it is crashing in Cmder and not in the other terminals is that Cmder is based on ConEmu, and ConEmu injects `conemuhk64.dll` which hooks console output. While the `printf` implementation in `ucrt` was not bothered by the misalignment, part of an (optimized) copy routine in `conemuhk64.dll` was (it is crashing on a `movaps xmmword ptr ds:[rax-0x38], xmm6` instruction). – CherryDT Feb 01 '22 at 15:26
  • Ok, makes sense. I will have to read about the stack alignment. Thank you for the help! – yonos bjørn Feb 01 '22 at 15:55

0 Answers0