Recent Posts

Pages: 1 ... 8 9 [10]
91
Programming with NASM / Re: Help with Hello World and NASM on mingw64
« Last post by alCoPaUL on January 07, 2023, 01:07:30 AM »
well i can speak for windows coz i delved on windows assembly doing a short personal project.

when you throw an assembly code (win32 or variants) to a disassembler (ida64), it doesn't break down the printf() to pieces, as you were implying.

you can step into the printf() but that's already the code referencing the function and going through it in the msvcrt.dll.

but i might be mistaken, tho. i don't do linux programming or any linux "virtual machine to windows" programming such as ming64 as you were using linux code to explain your side. the OP was using linux assembly so. :D
92
Programming with NASM / Re: Help with Hello World and NASM on mingw64
« Last post by fredericopissarra on January 07, 2023, 12:32:46 AM »
You are not understanding what I've said.. "C Runtime" object file is the initialization file linked to EVERY executable (those extra 6 KiB). A lot of initialization is made for a C program to work and, since the "assembly" (in disguise) program uses libc functions, the initialization must be linked to your code...

There is also the MSVCRT.DLL and other files used by your code (take a look with ldd utility). In my exemple, after loaded, the program uses, let's say, 1 KiB of RAM, your C disguised as "assembly" program uses way more...

And I want to deploy a program with NO C runtime dependency and I do not use Visual Studio (argh!). On Windows platform the principle is the same: You can import Win32 API routines, which are independent from C Runtime. This will instruct the linker to load the DLLs using early binding. And will work on EVERY single one of Windows versions, since Windows NT or Windows 95 until Windows 11. No C Runtime needed.

And no... Pure assembly code will always be smaller than mixed asm & C Runtime initialization routines... For the same code, I mean...
93
Programming with NASM / Re: Help with Hello World and NASM on mingw64
« Last post by alCoPaUL on January 06, 2023, 11:53:15 PM »
so if you just want to deploy a code that is heavy on C runtime usage on a certain OS (Windows 10 for example), get the appropriate Visual Studio version that builds Windows 10 programs by default. you won't concern or concern less if your code will run on Windows 10 coz it will surely can.

or just call the equivalent native APIs to do the same thing as your runtime dependent code and likely your code will be backwards-compatible.
94
Programming with NASM / Re: Help with Hello World and NASM on mingw64
« Last post by alCoPaUL on January 06, 2023, 11:01:44 PM »
^^ seems to be reversed in windows coz usually a portable win32(x64/x86_32) file is larger than any code that has functions that are dependent on outside libraries.

like you even need to install runtimes if you just wanna make your small program to execute a basic routine.

and yes, in some instances, a code could be smaller if it directly calls the built-in dlls like kernel32.dll or ntdll.dll.

but generally, for hlls, you need to install the language runtimes and if you want your code to run on a computer without the runtime, you have to make your code carry the runtime itself (or something like that)/
95
Programming with NASM / Re: Help with Hello World and NASM on mingw64
« Last post by fredericopissarra on January 06, 2023, 07:36:17 PM »
Here's my opinion... this is NOT assembly... this is C, disguised as assembly...
Let's look at both codes:
Code: [Select]
/* test.c */
int main(void){puts("Hello");return0;}
Code: [Select]
; test64.asm
  bits 64
  default rel

  section .text

  extern puts

  global main
  align 4
main:
  sub rsp,8    ; aligning the stack, as per SysV ABI.
  lea  rdi,[hello]
  call  puts wrt ..plt
  add rsp,8
  ret

  section .rodata

msg:
  db 'Hello',0
Now compile both:
Code: [Select]
$ gcc -O2 -s -o test test.c
$ nasm -felf64 -o test64.o test64.asm
$ gcc -s -o test64 test64.o
$ ls -go test*
-rwxr-xr-x 1 14416 jan  6 16:23 test
-rwxr-xr-x 1 14416 jan  6 16:23 test64
-rw-r--r-- 1   201 jan  6 16:23 test64.asm
-rw-r--r-- 1   880 jan  6 16:23 test64.o
-rw-r--r-- 1    59 jan  6 16:28 test.c
There's NO difference!
This:
Code: [Select]
; test64-2.asm
  bits  64
  default rel

  section .text

  global _start
  align 4
_start:
  mov   eax,1
  mov   edi,eax
  lea   rsi,[hello]
  mov   edx,hello_len
  syscall

  mov   eax,60
  xor   eax,eax
  syscall

  section .rodata

hello:
  db  `Hello\n`
hello_len equ $ - hello
Code: [Select]
$ nasm -felf64 -o test64-2.o test64-2.asm
$ ld -s -o test64-2 test64-2.o
$  ls -go test64-2*
-rwxr-xr-x 1 8480 jan  6 16:31 test64-2
-rw-r--r-- 1  268 jan  6 16:30 test64-2.asm
-rw-r--r-- 1  880 jan  6 16:31 test64-2.o
Give you a image file almost 6 KiB smaller because all that code on c0.o (C Runtime) aren't being linked to your code.
96
Programming with NASM / Re: Help with Hello World and NASM on mingw64
« Last post by alCoPaUL on January 06, 2023, 05:12:29 PM »
^^

printf() works wonders with "%s".

say you wanna lazy convert a set of characters using xor and you wanna convert them back again but the "distance" of say "-" to "_" will produce non-displayable characters when converting them back or sometimes displaying unwanted spaces and such, just pass them to "%s" and it will be in pure text.. it's like the clean up process for text conversion..

also print() is the first ever c routine that was used to hello world everyone done by brian kernighan..

you just push the text and push to "%s" and then call printf().

97
Programming with NASM / Re: Whats a practical use for lea
« Last post by Frank Kotler on January 06, 2023, 04:00:18 PM »
LEA uses the classroom to teach itself to students so the students can use it outside of the classroom.

What's really on your mind, Elawig57?

Frank

98
Programming with NASM / Re: Whats a practical use for lea
« Last post by fredericopissarra on January 06, 2023, 10:45:25 AM »
I just want to know about How is Lea used in the classroom?
And I gave you 3 uses...
99
I am pretty sure this is an ASSEMBLY forum...
Which operating system? It is pretty easy with Linux...
Code: [Select]
; readstr64.asm
;
;    $ nasm -felf64 -o readstr64.o readstr64.asm
;    $ ld -s -o readstr64 readstr64.o
;
  bits  64
  default rel

  %macro writestr 1
    mov   eax,1
    mov   edi,eax
    lea   rsi,[%1]
    mov   edx,%1_len
    syscall
  %endmacro

  %macro readstr 1
    xor   eax,eax
    mov   edi,eax
    lea   rsi,[%1]
    mov   edx,%1_len
    syscall
  %endmacro

  %macro exit 1
    mov   eax,60
    mov   edi,%1
    syscall
  %endmacro

  section .text

  global _start

_start:
  writestr prompt
  readstr str

  ; compare strings
  lea   rsi,[str]
  lea   rdi,[mask]
  mov   ecx,str_len
  rep   cmpsb
  je    .equal
 
  writestr different
  jmp   .exit

.equal:
  writestr equal_to

.exit:
  exit 0

  section .rodata

prompt:
  db  'Enter string: '
prompt_len equ $ - prompt

mask:
  db  '10110101'

different:
  db  `It's not the same!\n`
different_len equ $ - different

equal_to:
  db  `You got it!\n`
equal_to_len equ $ - equal_to

  section .data

str:
  ; This is now valid in NASM 2.15/2.16
  db 8 dup (0)
str_len equ $ - str
100
Use:

fgets (name, 100, stdin);
100 is the max length of the buffer. You should adjust it as per your need. MiOcado

Use:

scanf ("%[^\n]%*c", name);
The [] is the scanset character. [^\n] tells that while the input is not a newline ('\n') take input. Then with the %*c it reads the newline character from the input buffer (which is not read), and the * indicates that this read in input is discarded (assignment suppression), as you do not need it, and this newline in the buffer does not create any problem for next inputs that you might take.

Thanks,
Pages: 1 ... 8 9 [10]