CoddityTeam / movaicode

Concours mensuel du Pire Développeur de France
123 stars 10 forks source link

ASM / Ficelle #3

Closed Pascal-Coddity closed 2 years ago

Pascal-Coddity commented 4 years ago
sys_write   equ 1       ; the linux WRITE syscall
sys_exit    equ 60      ; the linux EXIT syscall
sys_stdout  equ 1       ; the file descriptor for standard output (to print/write to)

section .text
global _start
_start:
    pop r8          ; pop the number of arguments from the stack
    pop rsi         ; discard the program name, since we only want the commandline arguments
    pop rsi         ; 1st argument
    mov r15, rsi
    call _strlen
    call _print_reverse
exit:
    mov rax,    sys_exit    ; load the EXIT syscall number into rax
    mov rdi,    0       ; the program return code
    syscall             ; execute the system call

_strlen:
  ; string in rsi, result in rax
  push  rcx            ; save and clear out counter
  xor   rcx, rcx

_strlen_next:
  cmp   [rsi], byte 0  ; null byte yet?
  jz    _strlen_null   ; yes, get out

  inc   rcx            ; char is ok, count it
  inc   rsi            ; move to next char
  jmp   _strlen_next   ; process again

_strlen_null:

  mov   rax, rcx       ; rcx = the length (put in rax)

  pop   rcx            ; restore rcx
  ret                  ; get out

_print_1:
    mov rax,    sys_write   ; set the rax register to the syscall number we want to execute (WRITE)
    mov rdi,    sys_stdout  ; specify the file we want to write to (standard output in this case)
    mov rdx,    0x1     ; specify the (fixed) length of the string we want to print
    ; rsi
    syscall             ; execute the system call
    ret

_print_reverse:
    _debut:
        dec rsi
        cmp r15,rsi
        je _fin
        call _print_1
        jmp _debut
    _fin:
    ret
nasm -f elf64 -F dwarf -o ficelle.o ficelle.asm  && ld -o ficelle ficelle.o && ./ficelle " seiarv ses"
louismarslen commented 3 years ago

@Pascal-Coddity j'ai l'honneur solennel de t'annoncer que tu est le plus Movai Développeur de Coddity ! On te donnera ton trophée en main propre quand tu passeras au bureau 🎉

En attendant, un petit avant goût : IMG_2639

louismarslen commented 2 years ago

Je fais un peu de ménage dans les issues -> d'ailleurs go participer à l'édition 5