Author Topic: Print char to stdout  (Read 6257 times)

Offline Beta

  • Jr. Member
  • *
  • Posts: 3
Print char to stdout
« on: July 02, 2020, 05:14:50 PM »

How do print 'h' to stdout?
Why dont it print ?

Thank

Code: [Select]



section .text                           ;#section used for keeping the actual CODE
        global _start                   ;#declare the entry point


_start:                                 ;#Linker entry point
; Welcome message

        mov     edx,len                 ;#store msg lenght in EDX
        mov     ecx,msg                 ;#store msg ecx

        call    _printText              ;#prints 'hello, world !'




;syscall n° 4 (sys_write) ecx= const char * , edx = size_t,ebx = file descriptor unsigned int

        mov     ecx,'h'                 
;       mov     [num],ecx               ;


        add     ecx,48
        mov     edx,2                   ;#(size_t)
        mov     ebx,1                   ;#fb =  1
        mov     eax,4                   ;#sys_call n°
        int     0x80                    ;#call the KERNEL

        mov     eax,1                   ;#system call number (sys_exit)
        int     0x80                    ;#call the kernel


_printText:

        push    edx                     ;store edx into STACK (len)
        push    ecx                     ;store ecx into STACK (body text)

        mov     ebx,1                   ;#store fb(file descriptor) = STDOUT in EBX
        mov     eax,4                   ;#by direct adressing store the system call number in EAX (sys_write)
        int     0x80                    ;#call the KERNEL

        pop     edx                     ;#from stack to edx (recover)
        pop     ecx                     ;#from stack to ecx (recover)

        ret




section .data                           ;#section used for declaring INITIALIZED  DATA or costant (static memory)

msg         db  'hello, world !',0xA    ;#the welcome message
len         equ $ - msg                 ;#'actual address' minus 'msg address' = message's lenght

section .bss                            ;#unitialized static memory section that contains data declared later in the
                                        ;program. ZERO-FILLED BUFFER

num         resb 2                      ;#reserve 2 byte (read input number)




Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Print char to stdout
« Reply #1 on: July 02, 2020, 09:28:46 PM »
Hi beta,

It doesn't print because you don't have the address of "num" in ecx.

You only reserve 2 bytes for "num" and then you put ecx (4 bytes) in it. Since there's nothng after "num", this won't do any harm... but it isn't what you want. If there were another variable, it would get clobbered.

You add 48 to ecx... as if you were converting from the number 1 to the character '1'. I din't think that's what you're trying to do here. (?)

Code: [Select]
mov cl, 'h'
mov [num], cl ; one byte into (contents of) [num]
mov ecx, num ; address if num
mov edx, 1 ; just one byte?
mov ebx, 1 ; stdout
mov eax, 4 ; sys_write
int 80h

Something like that?

I sometimes do something like...

Code: [Select]
mov al, 'a'
call putchar
;...
putchar:
; push other registers?
push eax ; it's our buffer
mov ecx, esp ; address of buffer
; etc.

Hope it helps...

Best,
Frank



Offline ben321

  • Full Member
  • **
  • Posts: 182
Re: Print char to stdout
« Reply #2 on: July 11, 2020, 11:11:46 AM »
Not only does it not print to the console, it actually crashes. Running it in the debugger OllyDbg, I see it's getting the error "Access violation when reading (FFFFFFFF)". It would seem this may be part of Windows's protection system, blocking kernel-mode code from executing in user-mode programs. What was the target operating system here? Were you planning to use it in Windows 98 or something? It certainly is not working on Windows 10.

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2667
  • Country: us
Re: Print char to stdout
« Reply #3 on: July 12, 2020, 12:15:05 AM »
Hi Ben,

The last I wrote for Windows was for win98, but this is for Linux. The "int 80h" might have been a clue, but I should have specified.

Hope you are well. Be careful, but not fearful... everybody!

Best,
Frank