0

This might be a dumb question but I'm new to assembly programming and I've converted a .asm file containing the following assembly source code to a .exe file. The code was taken from https://www.tutorialspoint.com/assembly_programming/assembly_memory_management.htm by the way.

section .text
   global _main         ;must be declared for using gcc
    
_main:                   ;tell linker entry point

   mov  eax, 45      ;sys_brk
   xor  ebx, ebx
   int  80h

   add  eax, 16384   ;number of bytes to be reserved
   mov  ebx, eax
   mov  eax, 45      ;sys_brk
   int  80h
    
   cmp  eax, 0
   jl   exit    ;exit, if error 
   mov  edi, eax     ;EDI = highest available address
   sub  edi, 4       ;pointing to the last DWORD  
   mov  ecx, 4096    ;number of DWORDs allocated
   xor  eax, eax     ;clear eax
   std           ;backward
   rep  stosd            ;repete for entire allocated area
   cld           ;put DF flag to normal state
    
   mov  eax, 4
   mov  ebx, 1
   mov  ecx, msg
   mov  edx, len
   int  80h      ;print a message

exit:
   mov  eax, 1
   xor  ebx, ebx
   int  80h
    
section .data
msg     db  "Allocated 16 kb of memory!", 10
len     equ $ - msg

I then ran the following codes in the command line and got no confirmation output: command line NASM and GCC commands

If I run it in an online compiler, I'm getting a confirmation message "Allocated 16 kb of memory!", whereas no message was prompted in the command line. Am I missing something? or what should I add. How will a know if 16kb was allocated? I've fiddled with this previous query but I still don't know how: Create an exe file in assembly with NASM on 32-bit Windows. I hope this makes sense.

Johnny
  • 1
  • 2
    Those `int 80h` instructions are Linux system calls. They won't work in a native Windows .exe, just fault like `int 123` or any other interrupt number that Windows doesn't set up for user-space to use. I doubt Windows even has a `brk` at all; call VirtualAlloc instead. I assume the online "compiler" you used was running it on a Linux server. Assembly language programs are not portable between OSes, *at all*. Only the pure computation part, not the OS interaction. – Peter Cordes Jun 14 '22 at 04:24
  • 1
    The comments on [Create an exe file in assembly with NASM on 32-bit Windows](https://stackoverflow.com/q/37407363) already explain this, and so does one of the answers. The accepted answer replaces the system calls with library function calls. – Peter Cordes Jun 14 '22 at 04:27
  • I tried to follow your suggestion and ran it in an Ubuntu cmd I downloaded from microsoft store and followed all the protocols to make it work. used `nasm -f elf64 file.asm` and `ld -s -o test test.o` but I get the error **Segmentation fault (core dumped)** Am I missing something? – Johnny Jun 14 '22 at 09:29
  • 1
    That's 32-bit code dealing with pointers. Besides the system-call issue ([What happens if you use the 32-bit int 0x80 Linux ABI in 64-bit code?](https://stackoverflow.com/a/46087731)), you'd be truncating 64-bit pointers to 32-bit if the system calls succeed. Of if it's WSL1, then `int 80h` itself will segfault. If you want to write x86-64 Linux code, use different registers and call numbers for the `syscall` instruction. Actually I suspect the latter, so even `nasm -felf32 test.asm` / `ld -m elf_i386 test.o` wouldn't help. Even in a 64-bit executable, a non-PIE would have a low-32 brk. – Peter Cordes Jun 14 '22 at 09:31
  • I'm really lost, I've tried researching and encountered these queries https://askubuntu.com/questions/1268319/ubuntu-18-04-lts-64-bit-issues-running-32-bit-execute-file-have-followed-previo but I still encounter installation errors. How do you think should I proceed? Thanks for the help btw. Greatly appreciated. – Johnny Jun 14 '22 at 13:30
  • If you're using WSL, see [Does WSL 2 really support 32 bit program?](https://stackoverflow.com/q/61300194) to make sure your VM is actually WSL2, which is necessary running 32-bit programs. For this program specifically, you're not even using libc, so truly just NASM and `ld` are necessary. Packages like `gcc-multilib` are irrelevant, the only thing that matters is kernel support for CONFIG_IA32_EMULATION to run a 32-bit static executable. `nasm` always supports `-felf32`, and a 64-bit build of `ld` always supports `-m elf_i386` to make 32-bit executables. – Peter Cordes Jun 14 '22 at 14:03

0 Answers0