home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.asm.x86      Ahh, the lost art of x86 assembly      4,675 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 3,126 of 4,675   
   aen@nospicedham.spamtrap.com to p1299721@nospicedham.protonmail.com   
   Re: sys_read from stdin   
   29 Nov 17 06:59:01   
   
   On Tue, 28 Nov 2017 19:12:49 +0000, Richmond   
    wrote:   
   >...   
   >I don't know what all that means. But I am using Linux. And NASM. I did   
   >get this working eventually, so I think it must be OK.   
   >...   
   You should do some error checking.  That makes you more confident why   
   it works.   
      
   I kept the code below as simple as possible to make the concept clear.   
   ####   
   [list -]   
   (Remove expansion when you know how the macro works,   
    so the listing doesn't get bloated)   
   %imacro  sys 1-7.nolist   
   %imacro   sys 1-7   
   %ifnempty %7   
    mov r9, %7   
   %endif   
   %ifnempty %6   
    mov r8, %6   
   %endif   
   %ifnempty %5   
    mov r10,%5   
   %endif   
   %ifnempty %4   
    mov rdx,%4   
   %endif   
   %ifnempty %3   
    mov rsi,%3   
   %endif   
   %ifnempty %2   
    mov rdi,%2   
   %endif   
             mov       rax,sys_%1   
             syscall   
   %endmacro   
   [list +]   
    nasm -g -F dwarf -f elf64 read.asm -o read.o -l read.lst   
    ld -o read read.o   
    ./read   
    echo $? (Should be 0 if no error occurs.)   
    And take a look at read.lst   
    Try strace ./read   
   NULL      equ       0   
   STDIN     equ       0   
   STDOUT    equ       1   
      
   sys_read  equ       0   
   sys_write equ       1   
   sys_exit  equ       60   
      
             section   .bss   
   buffer:   resb      128   
      
             section   .text   
             global    _start   
   _start:   nop   
      
             call      gchar   
             jc        exit_err   
             sys       write, STDOUT, buffer, 1   
             bt        rax,63   
             jc        exit_err   
            more code   
      
   exit_ok:  sys       exit, NULL   
   exit_err: mov       rdi,rax   
             neg       rdi   
             sys       exit   
      
   gchar:    sys       read, STDIN, buffer, 1   
             bt        rax,63    ; Carry set on error!   
             jc        .1   
            more code   
             clc                 ; Carry clear if ok!   
   .1:       ret   
   --   
   aen   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca