问题
I am trying to loop through a string in nasm and printing it back out with a space in between each character and incrementing any digit less than 9 by 1.
So for example; if I type in command line;
str1 Hello8
str1 1234
str1 9
The output should be;
H e l l o 9
1 2 3 4
9
I've managed to print out the first two arguments;
%include "asm_io.inc"
global asm_main
section .data
section .bss
section .text
asm_main:
enter 0, 0
pusha
mov eax, dword [ebp+8] ; argc
call print_int ; display argc
call print_nl
mov ebx, dword [ebp+12] ; address of argv[]
mov eax, dword [ebx] ; get argv[0] argument -- ptr to string
call print_string ; display argv[0] arg
call print_nl
mov eax, dword [ebx+4] ; get argv[1] argument -- ptr to string
call print_string ; display argv[1] arg
call print_nl
mov eax, dword [ebx+8] ; get argv[2] argument -- ptr to string
call print_string ; display argv[1] arg
call print_nl
popa
leave
ret
Thank you for time and support.
回答1:
Assuming there is a print_char
in asm_io.inc
that prints al
:
mov ebx, dword [ebp+12] ; address of argv[]
mov esi, dword [ebx+4] ; get argv[1] argument -- ptr to string
l: lodsb ; al = [esi++]
or al, al ; if 0 we reached end of string
jz end
; check if al is a digit
cmp al, '0'
jb continue
cmp al, '9'
jae continue
inc al ; al was '0'..'8', now '1'..'9'
continue:
call print_char ; print the char
mov al, ' '
call print_char ; print the space
jmp l
end:
call print_nl
来源:https://stackoverflow.com/questions/33964811/print-out-string-with-spaces