-1

我有这个汇编程序,我想要这个程序的对角线输出,但我不知道如何将制表符放入汇编中。

section    .text
    global _start         ;must be declared for using gcc

_start:                 ;tell linker entry point

    mov edx, len        ;message length
    mov ecx, msg        ;message to write
    mov ebx, 1          ;file descriptor (stdout)
    mov eax, 4          ;system call number (sys_write)
    int 0x80            ;call kernel

    mov eax, 1          ;system call number (sys_exit)
    int 0x80            ;call kernel

section .data

msg db  'Y',10,'O',10,'U',10,'S',10,'U',10,'F'  ;our dear string
len equ $ - msg         ;length of our dear string

我的程序的输出是:

Y
O 
U 
S
U 
F

输出应该是这样的:

Y
  O
    U
      S
        U
          F

有没有其他方法可以编写这个程序并获得这个输出?

4

3 回答 3

3

有没有其他方法可以做到这一点

当然有!你可以做任何你想做的事!由于您说您使用的是 Windows,但使用的是 Linux 中断,因此此代码是 OS Neutral(意味着它可以在 Windows 或 Linux 上运行)

extern exit, printf, malloc, free
global main

section .data
szText      db  "Gunner Diagonally!!"
Text_Len    equ $ - szText
fmtstr      db  "%s", 10, 0

section .text
main:

    push    Text_Len
    push    szText
    call    PrintDiagonal

    call    exit

;~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;~ PrintDiagonal - Prints text to terminal diagonally
;~ In: esp + 4 = address of text to print
;~     esp + 8 = length of string to print 
;~ Returns - Nothing
;~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PrintDiagonal:
%define Text_ dword [ebp + 8]
%define TextLen_ dword [ebp + 12]
%define _Buffer dword [ebp - 4]
%define _SpaceCount dword [ebp - 8]
%define _CurLine dword [ebp - 12]

    push    ebp
    mov     ebp, esp
    sub     esp, 4 * 3

    mov     eax, TextLen_
    add     eax, eax
    push    eax
    call    malloc
    add     esp, 4 * 1
    mov     _Buffer, eax

    mov     _SpaceCount, 1
    mov     _CurLine, 1

    mov     esi, Text_ 
.NextLine:    
    mov     edi, _Buffer
    mov     edx, _SpaceCount
    dec     edx
    jz      .SpaceDone

.SpaceStart:
    mov     ecx, _SpaceCount
    dec     ecx
.FillSpaces:
    mov     byte [edi], 32
    inc     edi
    dec     ecx
    jnz     .FillSpaces

.SpaceDone:    
    mov     al, byte [esi]
    mov     byte [edi], al
    mov     byte [edi + 1], 0
    push    _Buffer
    push    fmtstr
    call    printf
    add     esp, 4 * 2

    inc     esi
    add     _SpaceCount, 2
    mov     edx, TextLen_ 
    inc     _CurLine
    cmp     _CurLine, edx
    jng     .NextLine

    push    _Buffer
    call    free
    add     esp, 4 * 1

    leave
    ret     4 * 2

没有错误检查,当然你会添加你自己的。

胜利诊断 Linux诊断

我们获取字符串并在循环中添加正确的空格,然后打印。

于 2013-10-25T00:51:01.110 回答
3

你可以把你的msg

msg db  'Y',10,9,'O',10,9,9,'U',10,9,9,9,'S',10,9,9,9,9,'U',10,9,9,9,9,9,'F'  ;our dear string

9 是标签的 ascii。

ascii 字符表

于 2013-10-24T07:39:28.140 回答
0

只有 Windows(由于 DOS 遗留问题)将在 X 位置 0 处移动托架的 CR(回车)和在不改变托架 X 位置的情况下向下移动一行的 LF(换行)分开。

在 Linux 中,仅使用 LF 并且它同时执行以下操作:将托架向下和向左移动到 0。

为了在 Linux 中获得相同的对角线输出,您应该稍微作弊:

; replace dots with spaces.
msg db  'Y',10,'.O',10,'..U',10,'...S',10,'....U',10,'.....F' 
于 2013-10-24T08:42:16.160 回答