Print ARGC in NASM without printf

Any good NASM / Intel Assembly programmers? If so, I have a question for you!

Every tutorial I can find on the internet shows using β€œprintf” to print the actual ARGC value on the screen (fd: / dev / stdout). You cannot just print it with sys_write (), for example:

SEGMENT .data ; nothing here

SEGMENT .text ; sauce
        global _start
        _start:
                pop ECX ; get ARGC value
                mov EAX, 4 ; sys_write()
                        mov EBX, 1 ; /dev/stdout
                        mov EDX, 1 ; a single byte
                int 0x80
                mov EAX, 1 ; sys_exit()
                        mov EBX, 0 ; return 0
                int 0x80
SEGMENT .bss ; nothing here

When I run this, I get no output. I tried to copy the ESP to EBP and tried to use the [EBP + 4] byte, (I was told that the brackets would unlink the memory address).

I can confirm that the value compared to the constant works. For example, this code works:

pop ebp ; put the first argument on the stack
mov ebp, esp ; make a copy 
cmp byte[ebp+4],0x5 ; does it equal 5?
je _good ; goto _good, &good, good()
jne _bad ; goto _bad, &bad, bad()

When we pop the stack, we technically need to get the full number of arguments, no? Oh, by the way, I am compiling with:

nasm -f elf test.asm -o test.o
ld -o test test.o

, . , .

+4
2

2 .

  • , .
  • , .

- :

SEGMENT .text ; sauce
        global _start
        _start:
                mov ecx, esp        ; pointer to ARGC on stack
                add byte [esp], '0' ; convert to text assuming single digit
                mov EAX, 4 ; sys_write()
                mov EBX, 1 ; /dev/stdout
                mov EDX, 1 ; a single byte
                int 0x80
                mov EAX, 1 ; sys_exit()
                mov EBX, 0 ; return 0
                int 0x80
+3

, ! , ! @Jester,

SEGMENT .text ; sauce
        global _start
        _start:
                mov ecx, esp        ; pointer to ARGC on stack
                add byte [esp], '0' ; convert to text assuming single digit
                mov EAX, 4 ; sys_write()
                mov EBX, 1 ; /dev/stdout
                mov EDX, 1 ; a single byte
                int 0x80
                mov EAX, 1 ; sys_exit()
                mov EBX, 0 ; return 0
                int 0x80

, . sys_write() , , "Hello World", "msg" - , .

SECTION .data ; initialized data
    msg: db "Hello World!",0xa
SECTION .text ; workflow
    global _start
    _start:
        mov EAX, 4
        mov EBX, 1
        mov ECX, msg ; a pointer!

, ECX

mov ecx, esp ; ecx now contains a pointer!

, "0" char , ESP ( ARGC), de-referencing , [ESP], ,

add byte[esp], '0' ; update the value stored at "esp"

, ! < 3

+1

Source: https://habr.com/ru/post/1613241/


All Articles