defining an array in 16bit assembly nasm

2»

Comments

  • don't believe i'm any help here
    i've always call C routines for input / output, hopefully someone will come around that knows more than i do
  • If you take a look at your assignment, you'll see that GetString should take di as a parameter for where the array is and PutString should use si. Also, you were told to use the special string instructions ([link=http://faydoc.tripod.com/cpu/lodsb.htm]lodsb[/link]/[link=http://faydoc.tripod.com/cpu/stosb.htm]stosb[/link]) instead of mov. I'd suggest that you try to rewrite the functions accordingly and ask if you get stuck :)

    Also, if a machine is 16-bit, then that doesn't mean you can't address anything smaller than that. On the x86, the smallest addressable unit is always a byte. One character is as big as one byte (8 bits), so all that multiplication by 2 really isn't necessary.
  • right thanks alot for the input im taking the day off from assembler i will continue tomorrow i will get back to you on my progress ect ect

    thanks alot for all your help

    what u mean about di and si ?
  • Di and si are registers, just like ax, bx, cx, etc. Lodsb/stosb are designed to work with these registers, that's why you were asked to use these registers as parameters to the string functions.
  • right got ya i will start on this again 2moz with all this in mind thank you :):):):) you have all helped me learn a fare bit thanks sooo much
  • the specs say
    "
    Remember to use the specialist string (array) instructions
  • A string and a character array are essentially the same thing. In higher languages like C there are differences between data types (a string ends with 0, but a character array might not), but in assembly any piece of data in memory is just an array of bytes, because assembly typically does not use the concept of data types. So lodsb/stosb can be used for any kind of data, they just happened to be most practical as string instructions, that's why they are called that way.

    Here's how I'd do the GetString function (well, not really, but idea wise, anyway :)
    [code]
    ; in main.asm
    mov di, MyArray
    call GetString


    ; in stdio.asm
    GetString:

    call Getch ; get the character stored in DL

    cmp dl, 0dh ; if Enter is pressed Exit the subroutine
    je Return

    call Putch ; output the character on screen

    stosb ; store the character in al to [di] and increment di by 1
    ; quite simple, isn't it? :)

    JMP GetString ; loop back to GetString

    Return:
    mov al, 0 ; terminate array with a 0
    stosb
    Ret


    Getch:
    push di
    mov ah, 7 ; keyboard input subprogram without echo
    int 21h ; read the character into al
    mov dl, al
    pop di
    RET ; return

    Putch:
    push di
    mov ah, 2h ; display subprogram
    INT 21H ;read the characters from al
    pop di
    RET ; Return
    [/code]
  • yeap it is easy however i have to make a program to the specs so no cutting corners really . It says "The input string should be placed in an array 256 bytes long" i dont really understand what that means do i have to define an array and write to it i just dont really know .

    also the 2nd part says "outputs to the console window the contents of a character array. The address of the start of the array should be passed to the subroutine in SI. "
  • after some research and messing around i dont understand why i cant output the string im using the code that anthrax posted with this subroutine for outputting the string :

    [code]

    PutString:
    cmp dl, 00h ; if 00h is found = end of string
    je Return2

    lodsb ; store the character in al to [di] and increment di by 1

    JMP GetString ; loop back to GetString

    Return2:
    Ret
    [/code]

    i get nothing on the screen :(
  • this probably isn't the problem but since characters only need a byte
    change your array from "MyArray resw 256" to MyArray resb 256" for a 256 byte array if you havn't done so already.
  • yea i have been messing and still cant output the array using lodsb, thanks i have changed the way i define the array now :)
  • Lodsb is basically equivalent to:
    [code]
    mov al, [si]
    inc si
    [/code]
    it has nothing to do with putting text on the screen, you still need to use the Putch routine for that.

    So here's my version of PutString. Remember you need to load the address of MyArray to si as a parameter for this to work. Putch should also be changed to not mess with si, because with interrupts you never know what they might change, just add "push si" / "pop si".
    [code]
    PutString:

    lodsb ; load the character in [si] to al and increment si by 1

    cmp al, 0
    jz Return2

    mov dl, al
    call Putch

    jmp PutString ; loop back to PutString

    Return2:
    Ret
    [/code]
  • agh right i got ya right soo far i have bunged all the code which has been posted into 1 file and this is what i get:
    [code]
    BITS 16 ;Set code generation to 16 bit mode
    ORG 0x0100;
    SECTION .text;



    MAIN:
    mov di, MyArray
    call GetString
    call Putln
    call PutString





    GetString:

    call Getch ; get the character stored in DL

    cmp dl, 0dh ; if Enter is pressed Exit the subroutine
    je Return

    ;call Putch *commeted out to see if putsring works* ; output the character on screen

    stosb ; store the character in al to [di] and increment di by 1
    ; quite simple, isn't it? :)

    JMP GetString ; loop back to GetString

    Return:
    mov al, 0 ; terminate array with a 0
    stosb
    Ret




    PutString:
    cld
    lodsb ; load the character in [si] to al and increment si by 1

    cmp al, 0
    jz Return2

    mov dl, al
    call Putch

    jmp PutString ; loop back to PutString

    Return2:
    Ret


    Getch:
    push di
    mov ah, 7 ; keyboard input subprogram without echo
    int 21h ; read the character into al
    mov dl, al
    pop di
    RET ; return

    Putch:

    push di
    mov ah, 2h ; display subprogram
    INT 21H ;read the characters from al
    pop di

    RET ; Return

    Putln: ;new line
    mov dl, 0DH ;Carriage return
    int 21h
    mov dl, 0AH ;Line feed
    int 21H
    RET ; return


    Exit:
    MOV AH,04Ch ; Select exit function
    MOV AL,00 ; Return 0
    INT 21h ; Call the interrupt to exit


    SECTION .bss
    MyArray resb 256
    [/code]


    and well im getting strange outputs i input 1234 i get like playing card symbols comming out and a smily face . i dont think those where the charcters i put into the string array?.


  • As I said, use si as a parameter to PutString:
    [code]
    [color=Red]mov si, MyArray[/color]
    call PutString
    [/code]
    The Putln function should set the interrupt function code. You always want to be able to call your functions from any context (whatever the register values might be), so don't make assumptions about registers unless you're absolutely certain what they are.
    [code]
    Putln: ;new line
    [color=Red]mov ah, 2[/color]
    mov dl, 0DH ;Carriage return
    int 21h
    mov dl, 0AH ;Line feed
    int 21H
    RET ; return
    [/code]
    and jump to Exit after calling PutString, otherwise it'll continue to execute GetString. That should make it work.
  • Hi,

    This is a wonderful opinion. The things mentioned are unanimous and needs to be appreciated by everyone.

    robinson

    [url=http://www.bizoppjunction.com/business-forums]Business Forums[/url]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories