defining an array in 16bit assembly nasm - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

defining an array in 16bit assembly nasm

Hi im learning assembly and i have to make a program to the following specs:

[color=Red]a)
GetString
«13

Comments

  • fangxifangxi Posts: 13Member
    "MyArray times 256 db 0" will initialize and array of 256 elements to the value of 0
    if you want to declare a uninitialized array is should be some thing like
    "MyArray resw 256" in the .bss section

    accessing the elements on the array is much like the stack, since essentially an array is just a section of program memory, the first element of the array will be at MyArray, you move through the array according to the size of the memory location reserved. on a 16 bit system i believe this would be 2 representing 16 bits, so moving through the array can be as simple ass "add MyArray,2" then refering to [MyArray],"[MyArray+2], [MyArray+4]..etc" also works to refer to the values, or you can use some sort of index variable or register for it
    [MyArray + 2*esi] assuming esi is what your using to index it.

    hope i was some help.
  • fortune2kkfortune2kk Posts: 17Member
    thanks alot i now know how to setup my array :) i have been reading loads of stuff and looking on the net for ages right i now have that done

    right i have this:

    [code]GetString:

    call Getch ; get the character stored in DL
    call Putch ; output the character on screen

    MOV MyArray[CX], DL <------------------problem here

    INC CX ;add 1 to the counter
    JMP GetString ; loop back to GetString


    ;getch reads a character fromm keyboard then moved it to DL
    Getch:
    push BX
    mov ah, 7 ; keyboard input subprogram without echo
    int 21h ; read the character into al
    mov dl, al ; move al to dl
    pop BX
    RET ; return


    ;putch outputs the character in AL
    Putch:
    mov ah, 2h ; display subprogram
    INT 21H ;read the characters from al
    RET ; Return
    [/code]


    right i have a infinite loop in GetString and i have a counter CX and what i am trying to do now is put the value of DL into the array at the position of the counter so if CX=1 is will put the value of DL in array[1].

    so if i input qwerty the array will look like this :

    array[0] = q , array[1] = w array[2] = e array[3] = r array[4] = t array[5] = y

    and then when i want to print it out i can just have a similar loop but instead of input will be output.


    thankyou:)

  • fangxifangxi Posts: 13Member
    your infinite loop is probably being caused when you call a method
    many methods actually mess with registers without preserving the values
    fixing it should be as simple as push cx before the call and pop cx after the call

    its a good idea to assume a method might mess with your registers.

    i believe what you want for "mov MyArray[cx], DL" should be something like:
    "mov [MyArray + cx], DL", unless your using cx to index through the array, then it would be something like "mov [MyArray + 2*cx], DL"

    hope this helps solve your problem
  • fortune2kkfortune2kk Posts: 17Member
    i need the infinite loop because im gonna have it soo when enter is pressed it breaks out of the infinite loop , Carriage Return.

    i have tryed using the 2 lines of code you suggested ( "mov [MyArray + cx], DL", "mov [MyArray + 2*cx], DL" ) and i get the error "invalid effective address"

    Im using CX to point to my arry CX=1 then i will point to array[1]

  • fangxifangxi Posts: 13Member
    sorry for the ineffective code last time
    this should work
    [code]
    push bx
    lea bx, [MyArray+2*cx]
    mov word[bx], DL
    pop bx
    [/code]

    this "might" work
    [code]
    mov word[MyArray+2*cx], DL
    [/code]
  • fortune2kkfortune2kk Posts: 17Member
    nope those 2 dont work either :(:( ive tried loads of other ways im stuck now need to sort this out :( any other ideas. Thankyou for your help so far . im using nasmide to code and compile
    [code]
    push bx
    lea bx, [MyArray+2*cx]
    mov word[bx], DL
    pop bx
    [/code]
    invalid effectiveness of code

    [code]
    mov word[MyArray+2*cx], DL
    [/code]
    mismatch in operand size
  • fangxifangxi Posts: 13Member
    i'm confused as to the problem also
    could you post the entire code?
  • fortune2kkfortune2kk Posts: 17Member

    MAIN.ASM

    [code]
    BITS 16 ;Set code generation to 16 bit mode
    ORG 0x0100;
    SECTION .text;

    MOV CX, 00h ; Sets counter for the number converted to 0
    MOV DL, 00h ; Sets DL to 0

    MAIN:
    MOV CX, 00h ; Sets counter for the number converted to 0
    call GetString

    call Exit


    %include "STDIO.asm"

    [/code]


    STDIO.ASM
    [code]
    SECTION .text



    ;*************************************************************
    ;Subroutine Name: Getch
    ;Function: Obtains character input from the keyboard
    ;Entry Conditions: Character to be moved into DL
    ;Exit Conditions: None
    ;Exceptions: None
    ;*************************************************************
    Getch:
    push BX
    mov ah, 7 ; keyboard input subprogram without echo
    int 21h ; read the character into al
    mov dl, al ; move al to dl
    pop BX
    RET ; return


    ;*************************************************************
    ;Subroutine Name: Putch
    ;Function: Outputs a character at the cursor position
    ; in the console window by calling DOS INT21H
    ;Entry Conditions: Character to be output in DL
    ;Exit Conditions: None
    ;Exceptions: None
    ;*************************************************************
    Putch:
    mov ah, 2h ; display subprogram
    INT 21H ;read the characters from al
    RET ; Return


    ;*************************************************************
    ;Subroutine Name: Putln
    ;Function: Outputs a new line at the cursor position
    ; in the console window by calling DOS INT21H
    ;Entry Conditions: Character to be output in AH
    ;Exit Conditions: None
    ;Exceptions: None
    ;*************************************************************
    Putln: ;new line
    mov dl, 0DH ;Carriage return
    int 21h
    mov dl, 0AH ;Line feed
    int 21H
    RET ; return


    ;*************************************************************
    ;Subroutine Name: Exit
    ;Function: When called upon it will exit the program
    ;Entry Conditions: None
    ;Exit Conditions: None
    ;Exceptions: None
    ;*************************************************************
    Exit:
    MOV AH,04Ch ; Select exit function
    MOV AL,00 ; Return 0
    INT 21h ; Call the interrupt to exit




    GetString:


    call Getch ; get the character stored in DL
    call Putch ; output the character on screen
    mov word[MyArray+2*cx], DL





    INC CX ;add 1 to the counter
    JMP GetString ; loop back to GetString


    SECTION .bss
    MyArray resw 256

    [/code]



    hope this helps in STDIO i use Putch, Getch, the problem is in GetString

    getstring still needs work on im just trying to get DL into Array[CX]

  • fangxifangxi Posts: 13Member
    i got it to compile using this

    [code]
    all Getch ; get the character stored in DL
    call Putch ; output the character on screen
    push bx
    mov bx, 2
    imul bx, cx
    mov [MyArray+bx], dl
    pop bx
    [/code]
    probably not the preferred way to index, but should work?
    not sure why it was giving an invalid memory address using the other

    hope this solves your problem
  • fortune2kkfortune2kk Posts: 17Member
    okky thanks that seems to compile im sorry for pestering but could you explain what that snippet of code does so i can understand and learn from it . why is bx used and what does imul do ?

    thanks alot hopefully i can get this done by 2night :)
«13
Sign In or Register to comment.