Search code examples
arraysloopsfasm

FASM: How to input values in array using loop


This code doesn't work for me. My goal is to ask user string input, convert to capital letters, and store them one by one in an array, then output the characters(that is in all caps) back to the user. please help me :(

org 100h

mov bl, 0

mov ah, 9
mov dx, input
int 21h

again:
mov ah, 1
int 21h

sub al, 20h
mov [inp+bl], al
inc bl

cmp bl, 2
jle again

loops:
mov bl, 0

mov ah, 2
mov dl, [inp+bl]
int 21h

inc bl
cmp bl, 2
jle loops

mov ax, 4Ch
int 21h

input db 'Input: ',24h
output db 'Output: ',24h
inp db 20 dup(?), 24h

Solution

  • mov [inp+bl], al
    

    The main problem here is that you use an addressing mode that simply does not exist!
    You can quickly correct your code if you change every instance of BL into BX.

    mov bx, 0
    
    mov ah, 9
    mov dx, input
    int 21h
    
    again:
    mov ah, 1
    int 21h
    
    sub al, 20h
    mov [inp+bx], al
    inc bx
    
    cmp bx, 2
    jle again
    
    loops:
    mov bx, 0
    
    mov ah, 2
    mov dl, [inp+bx]
    int 21h
    
    inc bx
    cmp bx, 2
    jle loops
    

    sub al, 20h
    

    Perhaps you've over-simplified the code because this capitalization will of course only work if the user only types in small caps a..z and nothing else.