Author Topic: How to read a string using call read_int or call read_char and split to array?  (Read 11239 times)

Offline kingpoop

  • Jr. Member
  • *
  • Posts: 2
So i am new to assembler and am struggling trying to read a value such as 10001010 using either
call read_int
call read_char
and then taking that input from the keyboard and splitting into an array such as

Is this even possible?
thank you for any advice, and i appreciate any coding help.  Here is what i have so far.

%include "/usr/local/share/"

segment .data
    message       db "Enter code?: ",0
    var1          dd 123
    bitmask       dd 1,1,1,0,0,0,1,1
    mask          dd 11100011
segment .bss
    cook resd 8

segment .text
   global  asm_main

   push   ebp
   mov    ebp, esp
   ;********** CODE STARTS HERE **********

        mov eax, 0
        mov ebx, 0
        cmp ebx, 8
        jge endinit
        mov DWORD [cook + ebx * 4], eax
        inc ebx
        jmp topinit

        cld ;clear destination flag
        mov eax, message        ;display message
        call print_string
        ;call read_char
        call read_int

        mov DWORD [var1], eax

        call read_char  ;\n

        mov eax, DWORD [var1]
        dump_regs 1
        call print_int

        jmp endloop2
        call print_nl

   ; *********** CODE ENDS HERE ***********
   mov      eax, 0
   mov      esp, ebp
   pop      ebp

Offline James2M

  • Jr. Member
  • *
  • Posts: 8

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


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.

« Last Edit: January 09, 2023, 05:08:29 AM by James2M »

Offline fredericopissarra

  • Full Member
  • **
  • Posts: 368
  • Country: br
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

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

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

  section .text

  global _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

  writestr equal_to

  exit 0

  section .rodata

  db  'Enter string: '
prompt_len equ $ - prompt

  db  '10110101'

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

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

  section .data

  ; This is now valid in NASM 2.15/2.16
  db 8 dup (0)
str_len equ $ - str
« Last Edit: January 06, 2023, 10:47:09 AM by fredericopissarra »