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

Offline kingpoop

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

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/asm_io.inc"

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

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


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

        ;;;;;;;;;;;;;;;;
        ;cook
        ;;;;;;;;;;;;;;;;
        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
       
                               
        endloop2:     
        call print_nl

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

Offline James2M

  • Jr. Member
  • *
  • Posts: 8
Use:

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

Use:

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.

Thanks,
« 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
    syscall
  %endmacro

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

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

  section .text

  global _start

_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

.equal:
  writestr equal_to

.exit:
  exit 0

  section .rodata

prompt:
  db  'Enter string: '
prompt_len equ $ - prompt

mask:
  db  '10110101'

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

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

  section .data

str:
  ; 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 »