简体   繁体   中英

how to print multiple strings on new line in the Assembly Language

I am trying to print multiple strings on a different line in Assembly but with my code, it keeps printing only the last string. I am very new to assembly language so, please bear with me

 section .text             
                           
 global _start             
 _start:                    
  mov edx, len              
  mov edx, len1             
  mov edx, len2             
  mov edx, len3             
  mov ecx, msg              
  mov ecx, str1             
  mov ecx, str2             
  mov ecx, str3             
  mov ebx, 1                
  mov eax, 4                
  int 0x80                  
  mov eax, 1                
  int 0x80                  

   
  section .data             


 msg  db 'Hello, world!',0xa                       
 str1 db 'Learning is fun!',0xa 
 str2 db 'I love beacon!',0xa         
 str3 db 'I love programming',0xa                      
 len1 equ $ - str1           
 len2 equ $ - str2           
 len3 equ $ - str3           
 len  equ $ - msg            

It only prints out I love programming.

It suppose to print

 Hello World!
 Learning is fun!
 I love beacon!
 I love programming
mov edx, len mov edx, len1

What do you expect?

You are overwriting the register edx .

This is just like the following code in other programming languages:

variableEdx = len;
variableEdx = len1;

The second line will overwrite the variable variableEdx and the effect of the first line will be gone!

how to print multiple strings

Function eax=4 writes some data in memory beginning at some address and ending at some address to some device.

If the second string immediately follows the first one in memory, you can send the memory consisting of both strings to the device.

Example:

...
mov edx, str1
mov ecx, 32
...

This will send 32 bytes of the content of the memory starting at str1 to the device. And 32 bytes starting at str1 are the strings str1 and str2 .

If you want to send multiple blocks of memory to a device, you could use the writev() system call, which is function eax=146 , instead. (See this link ).

Example:

.text
.globl _start

_start:
    mov edx, 3
    mov ecx, offset list
    mov ebx, 1
    mov eax, 146
    int 0x80
    mov eax, 1
    int 0x80

.data

list:
    .long msg
    .long 7
    .long str1
    .long 8
    .long str3
    .long 19

    ...

Unfortunately, the assembler I use has a slightly different syntax than yours; however, in your assembly the list part would probably look like this:

list dd msg
     dd 7
     dd str1
     ...

writev (function 146) takes a pointer to some "list" in the ecx register and the number of entries in the list in the edx register.

Each entry in the list consists of two 32-bit words. The first word is the address of the memory to be written to the device; the second word is the number of bytes to be written.

The example above writes " Hello, Learing I love programming ":

The first 7 bytes of "msg", then the first 8 bytes of "str1" and then all 19 bytes of "str3".

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