如何在汇编语言的新行上打印多个字符串 [英] how to print multiple strings on new line in the Assembly Language

查看:110
本文介绍了如何在汇编语言的新行上打印多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Assembly中的不同行上打印多个字符串,但是使用我的代码,它只打印最后一个字符串.我是汇编语言的新手,所以请多多包涵

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.

它应该打印

 Hello World!
 Learning is fun!
 I love beacon!
 I love programming

推荐答案

mov edx, len              
mov edx, len1             

您期望什么?

您正在覆盖寄存器 edx .

这就像其他编程语言中的以下代码:

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

variableEdx = len;
variableEdx = len1;

第二行将覆盖变量 variableEdx ,第一行的效果将消失!

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

如何打印多个字符串

how to print multiple strings

函数 eax = 4 将一些数据从某个地址开始到某个设备的某个地址结束写入内存.

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.

示例:

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

这将从 str1 开始将32字节的内存内容发送到设备.从 str1 开始的32个字节是字符串 str1 str2 .

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.

如果要将多个内存块发送到设备,则可以使用 writev()系统调用,该调用是函数 eax = 146 .(请参见此链接).

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).

示例:

.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

    ...

不幸的是,我使用的汇编器的语法与您使用的语法略有不同.但是,在您的程序集中, list 部分可能看起来像这样:

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 (功能146)采用指向某些列表"的指针. ecx 寄存器中的值和 edx 寄存器中列表中的条目数.

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.

列表中的每个条目都包含两个32位字.第一个字是要写入设备的存储器的地址.第二个字是要写入的字节数.

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.

上面的示例写着"你好,我喜欢编程的学习":

"msg"的前7个字节,然后是"str1"的前8个字节.然后是所有19个字节的"str3".

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

这篇关于如何在汇编语言的新行上打印多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆