Assembler Developer

Moderators: None (Apply to moderate this forum)
Number of threads: 970
Number of posts: 1762

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 1:58 PM
Hi im learning assembly and i have to make a program to the following specs:

a)
GetString – inputs a string with echo (into a string in memory i.e. an array of char) from the keyboard until it encounters a cariage return character (note – not a line feed). This is because the <enter> key on the keyboard is encoded as a single carriage return character (not carriage return/linefeed). Thus you cannot look for linefeed as the input termination character.

The carriage return character you have found should not be stored with the rest of the string, it should be discarded. The input string should be placed in an array 256 bytes long. The start address of the destination character array should be passed as a parameter to the subroutine in DI. Very importantly you should ensure that the string of characters you have input is null terminated i.e. as in C put a byte value of 00H into the first byte after the end of the string. The subroutine should not corrupt any register values.

Remember to use the specialist string (array) instructions – although we are unable in this instance to use rep.

Also note that you can use the subroutine Getch that you wrote last week to input individual characters (it should be in stdio.asm).

b) -

PutString – 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. The subroutine assumes that the string is null terminated. You can use the subroutine Putch that you wrote last week to output the individual characters.

c) Write some code in a separate program to test your subroutines. NOTE – PutString like PutHex does NOT output a newline so you will need to do this yourself if you want to put it on separate lines.



im guessing you use a character array and loop for input and output


the problem i am having is that i dont know how to define the array i have read threw the material given and cant find nothing so i searched on the Internet and found this

"MyArray times 256 db 0 "


will this allow me to input up to fill the array with 256 characters / bytes if not how would i go about doing this


my second question is how will i write to the array say i have the character 'a' sitting in the DL register how would i move it to MyArray[CX] (MyArray[0]) im guessing g would it be something like MOV MyArray[CX], DL ... am i on the right lines ??


thanks alot :)




Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 28 Mar 2009 at 2:11 PM
"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.
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 2:33 PM
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:

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



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:)

Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 28 Mar 2009 at 2:40 PM
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
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 2:53 PM
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]

Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 28 Mar 2009 at 3:23 PM
sorry for the ineffective code last time
this should work
push bx
lea bx, [MyArray+2*cx]
mov word[bx], DL
pop bx


this "might" work
mov word[MyArray+2*cx], DL

Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 3:31 PM
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
push bx
lea bx, [MyArray+2*cx]
mov word[bx], DL
pop bx

invalid effectiveness of code

mov word[MyArray+2*cx], DL

mismatch in operand size
Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 28 Mar 2009 at 3:35 PM
i'm confused as to the problem also
could you post the entire code?
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 3:38 PM

MAIN.ASM

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"




STDIO.ASM
 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





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]

Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 28 Mar 2009 at 3:54 PM
i got it to compile using this

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

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
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 4:13 PM
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 :)
Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 28 Mar 2009 at 4:25 PM
not a problem i suppose i could comment it real fast

essentially the changes just do the "2*cx" from our old code

bx is just being used as another memory location to aid in indexing
you could probably use any unused registers in its place, or a variable

push bx ; preserve value on the stack

mov bx, 2 ; bx = 2
imul bx, cx ; imul is an assembly command for multiplication
            ; the way it was used here is bx = bx*cx

mov [MyArray+bx], dl  ; bx is being used as the index here for your array
 
pop bx ; restore previous bx value





hope this helped
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 4:30 PM
ok sooo if i was to input hello at MyArray[1] it will hold e and MyArray[3] will hold l ? is that what the code is doing.
sorry im new to assembler
Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 28 Mar 2009 at 5:17 PM
if the values your passing in through dl are the characters then yes, it should be like you said

and its ok, i'm relatively new myself
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 5:20 PM
okky sounds what im after well then now time to print the characters out :)
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 28 Mar 2009 at 5:56 PM
Right now im having a bit of a problem with the prinitng out of the charters. It seems to read the characters into the array but when the PutString is called nothing it echo'd

main.asm
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 to 0
call GetString

;add black lines here

MOV CX, 00h ; Sets counter to 0
Call PutString

call Exit
		
		
%include "STDIO.asm"




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

push bx ; preserve value on the stack
mov bx, 2 ; bx = 2
imul bx, cx ; imul is an assembly command for multiplication
            ; the way it was used here is bx = bx*cx

mov [MyArray+bx], dl  ; bx is being used as the index here for your array
pop bx ; restore previous bx value


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






PutString:
push bx ; preserve value on the stack
mov bx, 2 ; bx = 2
imul bx, cx ; imul is an assembly command for multiplication
            ; the way it was used here is bx = bx*cx

mov AL,[MyArray+bx]  ; put the value of the array back into AL
call Putch ; output the character on screen
pop bx ; restore previous bx value


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














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:
mov ah, 2h ; display subprogram
INT 21H ;read the characters from al
RET ; Return



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


any ideas everything compiles fine but i get no output from the PUtString subroutine

Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 28 Mar 2009 at 7:55 PM
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
Report
Re: defining an array in 16bit assembly nasm Posted by anthrax11 on 29 Mar 2009 at 4:06 AM
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 (lodsb/stosb) 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.
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 29 Mar 2009 at 4:56 AM
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 ?
Report
Re: defining an array in 16bit assembly nasm Posted by anthrax11 on 29 Mar 2009 at 6:34 AM
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.
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 29 Mar 2009 at 7:49 AM
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
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 30 Mar 2009 at 11:31 AM
the specs say
"
Remember to use the specialist string (array) instructions – although we are unable in this instance to use rep.
"
but you say yo use "lodsb/stosb" , arnt those commands for storing in a string and stuff i need to store in a character array

Report
Re: defining an array in 16bit assembly nasm Posted by anthrax11 on 30 Mar 2009 at 12:54 PM
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 :)
; 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
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 30 Mar 2009 at 1:39 PM
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. "
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 30 Mar 2009 at 4:10 PM
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 :


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


i get nothing on the screen :(
Report
Re: defining an array in 16bit assembly nasm Posted by fangxi on 31 Mar 2009 at 6:46 AM
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.
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 31 Mar 2009 at 6:49 AM
yea i have been messing and still cant output the array using lodsb, thanks i have changed the way i define the array now :)
Report
Re: defining an array in 16bit assembly nasm Posted by anthrax11 on 31 Mar 2009 at 8:16 AM
Lodsb is basically equivalent to:
mov al, [si]
inc si

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".
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
Report
Re: defining an array in 16bit assembly nasm Posted by fortune2kk on 31 Mar 2009 at 9:04 AM
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:
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



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?.


Report
Re: defining an array in 16bit assembly nasm Posted by anthrax11 on 31 Mar 2009 at 9:59 AM
As I said, use si as a parameter to PutString:
mov si, MyArray
call PutString

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.
Putln: ;new line
mov ah, 2
mov dl, 0DH ;Carriage return
int 21h
mov dl, 0AH  ;Line feed
int 21H
RET ; return

and jump to Exit after calling PutString, otherwise it'll continue to execute GetString. That should make it work.
Report
Hi Posted by robinson123 on 24 Apr 2009 at 4:56 AM
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]


1 2  Next



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.