Author Topic: Macro for scanning user input into a string  (Read 506 times)

Offline T145

  • Jr. Member
  • *
  • Posts: 12
  • Country: 00
  • Hacker, Jacker & All-Around Gobsmacker
    • CodeReview
Macro for scanning user input into a string
« on: December 13, 2019, 01:24:42 AM »
Is there anything that can be improved here?

Code: [Select]
%macro newline 0.nolist
    mov     ax, 0E0Dh
    int     10h
    mov     al, 0Ah
    int     10h
%endmacro

%macro getc 0.nolist
%%get:
    mov     ah, 11h
    int     16h        ; get keystroke status
    jnz     %%pressed
    hlt
    jmp     %%get
%%pressed:
    mov     ah, 10h
    int     16h        ; get the pressed key on "modern" keyboards
%endmacro

; %1 = string to write into
%macro scan 1
    pushaw
    mov     di, %1     ; prep for stosb
    xor     cx, cx
%%get:
    getc
    cmp     al, 13
    je      %%done     ; enter pressed
    cmp     al, 8
    je      %%bspace
    cmp     al, ' '
    jb      %%get      ; ignore most non-printing characters
    cmp     al, '~'
    ja      %%get

    push    bp
    mov     ah, 0Eh    ; print the valid character
    int     10h
    push    bp

    stosb              ; write the character to the string
    inc     cx
    cmp     cx, 80     ; ensure the buffer isn't exceeded
    jae     near %%done
    jmp     near %%get ; room for more
%%bspace:
    cmp     cx, 0
    je      %%get      ; ignore starting bspace

    push    bp
    mov     ax, 0E08h
    int     10h        ; bspace twice, to clear space
    mov     al, 32
    int     10h
    mov     al, 8
    int     10h
    pop     bp

    dec     di         ; overwrite character position
    dec     cx         ; step back a character
    jmp     %%get
%%done:
    xor     ax, ax
    stosb              ; append null-terminating character (zero)
    newline
    popaw
%endmacro

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2395
  • Country: us
Re: Macro for scanning user input into a string
« Reply #1 on: December 13, 2019, 03:27:34 AM »

...
Code: [Select]
    push    bp
    mov     ah, 0Eh    ; print the valid character
    int     10h
    push    bp

Do you intend to push bp twice here?

You seem to ASSume you have 80 bytes in buffer. Should that be a macro parameter?

This seems a lot of code to stuff in every time you enter a string. Better as a subroutine?

Just thinkin'...

Best,
Frank


Offline T145

  • Jr. Member
  • *
  • Posts: 12
  • Country: 00
  • Hacker, Jacker & All-Around Gobsmacker
    • CodeReview
Re: Macro for scanning user input into a string
« Reply #2 on: December 13, 2019, 04:35:23 AM »
Good catch; fixed it! By "subroutine" I assume you mean just having the entire macro under a label and calling it. Would there be any benefit to doing so?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2395
  • Country: us
Re: Macro for scanning user input into a string
« Reply #3 on: December 13, 2019, 05:04:09 AM »
Quote
Would there be any benefit to doing so?

Just a trade-off between size and speed. Depends on how many times you use the macro. No point  in trying to make user input go fast (IMO) so I'd go with a subroutine. Your choice.

Best,
Frank


Offline T145

  • Jr. Member
  • *
  • Posts: 12
  • Country: 00
  • Hacker, Jacker & All-Around Gobsmacker
    • CodeReview
Re: Macro for scanning user input into a string
« Reply #4 on: December 13, 2019, 06:50:52 PM »
Oh, you mean if it's frequently used, then have it be a subroutine. That way, the entire code in the macro doesn't get pasted in every time it would've been called. Is this accurate?

Offline Frank Kotler

  • NASM Developer
  • Hero Member
  • *****
  • Posts: 2395
  • Country: us
Re: Macro for scanning user input into a string
« Reply #5 on: December 13, 2019, 09:51:32 PM »
Yes, exactly.

Best,
Frank