简体   繁体   中英

How to read each char from string NASM assembly 64bit linux

I have a 64bit NASM assembly assignment to capitalize (all letters should be lowercase,except those which are at the beginning of the sentence) letters of input text. I'm totally new to assembler and I can't find anywhere how I should read each char from string incrementally, when I read the text like this:

section .data

prompt      db  "Enter your text: ", 10
length      equ $ - prompt
text        times 255 db 0
textsize    equ $ - text    

section .text
global main
main:
    mov     rax, 1
    mov     rdi, 1
    mov     rsi, prompt
    mov     rdx, length
    syscall         ;print prompt

    mov     rax, 0
    mov     rdi, 0
    mov     rsi, text
    mov     rdx, textsize
    syscall         ;read text input from keyboard




exit:
    mov     rax, 60
    mov     rdi, 0
    syscall

Also, I'm not sure how to find out when the text is over, so I could know when I have to exit the program. Should I do some operations with text size or there is some king of special symbol which shows the EOL? Thank you for your answers.

After returning from sys_read (syscall rax=0) RAX register should contain the number of characters actually has been read. Notice, that in Linux, sys_read will return when /n is accepted, even if there is more place in the buffer provided.

Then organize a loop from 0 to RAX and process each character the way you want:

        mov    byte ptr [text+rax], 0  ; make the string zero terminated for future use.

        mov    rcx, rax  ; rcx will be the character counter.
        mov    rsi, text ; a pointer to the current character. Start from the beginning.

process_loop:
        mov    al, [rsi]   ; is it correct NASM syntax?

       ; here process al, according to your needs...
       ; .....

       inc    rsi
       dec    rcx
       jnz    process_loop

The above code can be optimized of course, for example to use string instructions or loop instructions, but IMO, this way is better for a beginner.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM