nasm中的数据库用法,尝试存储和打印字符串 [英] db usage in nasm, try to store and print string

查看:115
本文介绍了nasm中的数据库用法,尝试存储和打印字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在程序集中存储一些字符串,并使用WriteString在屏幕上打印出来.但是,我只调用其中一个字符串,它在一行中显示了所有字符串.为什么会这样?我的代码有什么问题吗?谢谢!

I was trying to store a few strings in my assembly, and use WriteString to print out on the screen. However, I only call one of the strings, it shows up all of them in one single line. Why does this happen? Is that anything wrong in my code? Thanks!

此处的代码:

%include "lib/libasm.h"
SECTION .text

global main

main:
    call badrng
    ret 


badrng:
    push eax
  push edx
    mov edx, badrngstr
    call WriteString
    pop edx
    pop eax
    call failure
    ret

failure:
    mov  eax,1
    int  0x80
    ret


SECTION .data
nlstr:      db ""
badrngstr:  db "Value out of range in assignment"
baddivstr:  db "Division by zero"
badcasstr:  db "Value not handled in case statement"
badptrstr:  db "Attempt to use a null pointer"
badsubstr:  db "Subscript out of bounds"
stkovstr:   db "Stack overflow"
heapovstr:  db "Out of heap space"

WriteString位于libasm.s

and WriteString is in libasm.s

    EXTERN  main

    SECTION .text
    GLOBAL  _start
_start:
    call    main
    mov ebx, eax
    call    Exit
    ret

    GLOBAL  AsciiDigit
AsciiDigit: ; al = digit to convert
    SECTION .data
.xtable db  "0123456789abcdef"
    SECTION .text
    push    ebx
    mov ebx, .xtable
    xlat
    pop ebx
    ret

    GLOBAL  Crlf
Crlf:
    SECTION .data
.lf db  0x0a
.lflen  equ $-.lf
    SECTION .text
    pusha
    mov ecx, .lf
    mov edx, .lflen
    call    Write
    popa
    ret

    GLOBAL  Exit
Exit:                   ; ebx = error code
    mov eax, 0x01
    int 0x80
    ret

    GLOBAL  Strlen
Strlen:                 ; edi = string
    push    edi
    mov eax, 0
.L1:
    cmp byte [edi], 0       ; end of string?
    je  .L2
    inc edi
    inc eax
    jmp .L1
.L2:
    pop edi
    ret

    GLOBAL  Write
Write:                  ; ecx = buffer, edx = count
    push    ebx
    mov eax, 0x04       ; sys_write = 4
    mov ebx, 1          ; fd = 1 (stdout)
    int 0x80
    pop ebx
    ret

    GLOBAL  WriteChar
WriteChar:              ; al = the char
    SECTION .data
.bb db  0
    SECTION .text
    pushad
    mov byte [.bb], al
    mov ecx, .bb
    mov edx, 1
    call    Write
    popad
    ret

    GLOBAL  WriteHex
WriteHex:               ; eax = the number
    SECTION .data
.buf    TIMES 16 db 0           ; buffer
.bufsz  equ $-.buf          ; bufsize
    SECTION .text
    pushad
    mov edi, .buf       ; point to the string
    add edi, .bufsz
    mov ecx, 0          ; buflen
.L1:
    mov ebx, eax        ; preserv eax into ebx
    and al, 0x0f
    call    AsciiDigit
    dec edi
    mov byte [edi], al
    inc ecx
    mov eax, ebx        ; restore eax
    shr eax, 4
    or  eax, eax        ; eax == zero ?
    jnz .L1
    ;
    mov edx, ecx
    mov ecx, edi
    call    Write
    popad
    ret

    GLOBAL  WriteInt
WriteInt:               ; eax = the number
    SECTION .data
.isneg  db  0           ; negative = 0 (false)
.buf    TIMES 16 db 0           ; buffer
.bufsz  equ $-.buf          ; bufsize
    SECTION .text
    pushad
    mov byte [.isneg], 0    ; negative = 0 (false)
    or  eax, eax        ; eax is positive?
    jns .L1
    neg eax
    mov byte [.isneg], 1    ; negative = 1 (true)
.L1:
    mov edi, .buf       ; point to the string
    add edi, .bufsz
    mov ecx, 0          ; buflen
    mov ebx, 10         ; divided by 10
.L2:
    mov edx, 0          ; edx:eax = the number
    div ebx         ; eax = Q, edx = R
    or  dl, 0x30        ; convert value to ASCII
    dec edi
    mov byte [edi], dl
    inc ecx
    or  eax, eax        ; eax == zero ?
    jnz .L2
    ; add the sign symbol
    cmp byte [.isneg], 0
    je  .L3
    dec edi
    mov byte [edi], '-'
    inc ecx
.L3:
    mov edx, ecx
    mov ecx, edi
    call    Write
    popad
    ret

    GLOBAL  WriteString
WriteString:
    SECTION .text
    pushad
    mov edi, edx
    call    Strlen
    mov ecx, edx
    mov edx, eax
    call    Write
    popad
    ret

,输出结果为

Value out of range in assignmentDivision by zeroValue not handled in case statementAttempt to use a null pointerSubscript out of boundsStack overflowOut of heap space

即使我只将edx的一个字符串移动,我也不知道为什么它将所有字符串打印出来.拜托,谁能告诉我我在哪里做错了?

I don't know why it would print all the string out, even though I only move one string the edx. Please, can anyone tell where did I do wrong?

注意:它已经过测试并在ubuntu 12.04 32bit上运行.

Note: It is tested and run on ubuntu 12.04 32bit.

推荐答案

字符串必须以null终止-您必须指出最后一个字节的零值.如果希望输出例程将字符串放在自己的行上,则可能还需要指示回车符和换行符.

The strings have to be null terminated -- you have to indicate the zero value for the last byte. You may also need to indicate carriage returns and line feeds if you want the output routine to put the string on it's own line.

您可以通过指定:

nullstr db 0
errorMessage db 'critical error', 10, 13, 0
warningMessage db 'warning', 10, 13, 0

goodbyeWorld1 db 'Goodbye World.', 0
goodbyeWorld2 db 'Goodbye World.'

endline db 10, 13, 0

endline字符串将基于以下原理工作:由于goodbyeWorld2并非以null终止,因此它将显示用于回车和换行的ascii代码,因此Goodbye World.会出现在shell输出中其自己的行上.但是,由于goodByeWorld1为空终止,因此它不会出现在自己的行上,并且输出将在最后的'.'处继续.显示的字符.

The endline string would work on the principle that since goodbyeWorld2 is not null terminated, it would display the ascii codes for carriage feed and line return, so Goodbye World. would appear on it's own line in the shell output. However since goodByeWorld1 is null-terminated it would not appear on it's own line and output would continue at the final '.' character displayed.

这篇关于nasm中的数据库用法,尝试存储和打印字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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