簡體   English   中英

Linux nasm程序集將字符附加到字符串

[英]Linux nasm assembly append character/s to a string

在Arch Linux中的NASM上,如何將字符零('0')附加到32位變量中? 我想要這樣做的原因是,我可以通過將一位數字輸入設置為1並附加一個零來輸出數字10。 我需要弄清楚如何附加零。

理想的情況:

Please enter a number: 9
10

使用這種方法,我希望能夠做到這一點:

Please enter a number: 9999999
10000000

我怎樣才能做到這一點?

提前致謝,

賴利

好吧,正如Bo所說的...但是我很無聊。 您似乎不願意通過簡單的方法執行此操作(將輸入轉換為數字,加1,然后將其轉換回文本),因此我嘗試使用字符。 這就是我想出的。 這很恐怖,但是“似乎可以工作”。

; enter a number and add 1 - the hard way!
; nasm -f elf32 myprog.asm
; ld -o myprog myprog.o -melf_i386

global _start

; you may have these in an ".inc" file
sys_exit equ 1
sys_read equ 3
sys_write equ 4
stdin equ 0
stdout equ 1
stderr equ 2
LF equ 10

section .data
    prompt db "Enter a number - not more than 10     digits - no nondigits.", LF
    prompt_size equ $ - prompt
    errmsg db "Idiot human! Follow instructions next time!", LF
    errmsg_size equ $ - errmsg

section .bss
    buffer resb 16
    fakecarry resb 1

section .text
_start:
    nop
    mov eax, sys_write
    mov ebx, stdout
    mov ecx, prompt
    mov edx, prompt_size
    int 80h

    mov eax, sys_read
    mov ebx, stdin
    mov ecx, buffer + 1 ; leave a space for an extra digit in front
    mov edx, 11
    int 80h
    cmp byte [buffer + 1 + eax - 1], LF
    jz goodinput

; pesky user has tried to overflow us!
; flush the buffer, yell at him, and kick him out!
    sub esp, 4 ; temporary "buffer"
flush:
    mov eax, sys_read
    ; ebx still okay
    mov ecx, esp ; buffer is on the stack
    mov edx, 1
    int 80h
    cmp byte [ecx], LF
    jnz flush
    add esp, 4 ; "free" our "buffer"
    jmp errexit

goodinput:
    lea esi, [buffer + eax - 1] ; end of input characters
    mov byte [fakecarry], 1 ; only because we want  to add 1
    xor edx, edx ; count length as we go

next:
; check for valid decimal digit
    mov al, [esi]
    cmp al, '0'
    jb errexit
    cmp al, '9'
    ja errexit

    add al, [fakecarry] ; from previous digit, or first... to add 1
    mov byte [fakecarry], 0 ; reset it for next time
    cmp al, '9' ; still good digit?
    jna nocarry

; fake a "carry" for next digit
    mov byte [fakecarry], 1
    mov al, '0'
    cmp esi, buffer + 1
    jnz nocarry

; if first digit entered, we're done
; save last digit and add one ('1') into the space we left
    mov [esi], al
    inc edx
    dec esi
    mov byte [esi], '1'
    inc edx
    dec esi
    jmp done

nocarry:
    mov [esi], al
    inc edx
    dec esi
    cmp esi, buffer
    jnz next

done:
    inc edx
    inc edx
    mov ecx, esi ; should be either buffer + 1, or buffer
    mov ebx, stdout
    mov eax, sys_write
    int 80h
    xor eax, eax ; claim "no error"

exit:
    mov ebx, eax
    mov eax, sys_exit
    int 80h

errexit:
    mov edx, errmsg_size
    mov ecx, errmsg
    mov ebx, stderr
    mov eax, sys_write
    int 80h
    mov ebx, -1
    jmp exit
;-----------------------------    

那是你的想法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM