Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jeremiah

asm question

This topic is 6650 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

why wont this produce the correct results? 1, 2, 3 large DOS 16-bit ------- #include #include #include unsigned char far *buffer; void main(void) { int result1 = 0, result2 = 0, result3 = 0; printf("Allocating 64K... "); if((buffer = (unsigned char far *)farmalloc(64000)) == NULL) { printf("Not enough memory\n"); exit(1); } printf("done\n"); for(int i=0; i<100; i++) buffer = i; asm { xor AX, AX mov BX, SEG buffer[0] mov ES, BX mov BX, OFFSET buffer[0] mov result1, [ES:BX] inc BX mov result2, [ES:BX] inc BX mov result3, [ES:BX] } printf("result1 is: %d\nresult2 is: %d\nresult3 is: %d\n", result1, result2, result3); farfree(buffer); printf("freed mem.\n"); }

Share this post


Link to post
Share on other sites
Advertisement
for(int i=0; i<100; i++) buffer = i;

should be something like this:

for(int i=0; i<100; i++) *(buffer+i) = i;

you were modifying the pointer to the allocated memory, not the actual memory it was pointing at.

Edited by - ao on 3/11/00 10:18:50 PM

Edited by - ao on 3/11/00 10:19:18 PM

Share this post


Link to post
Share on other sites
I must have put the code down wrong then...
you can actually write that line in a couple ways, with one way being:
for (int i = 0; i < 100; i++) buffer[ i ] = i;

anyway, I have inserted a line to print the first three chars of the array and the first 100 chars are being filled in with 0 to 99.

I am thinking my assembly is a bit wrong...

Heres the corrected source (large DOS 16-bit)
------

#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>

unsigned char far *buffer;

void main(void)
{
int result1 = 0, result2 = 0, result3 = 0;
printf("Allocating 64K... ");
if((buffer = (unsigned char far *)farmalloc(64000)) == NULL)
{
printf("Not enough memory\n");
exit(1);
}
printf("done\n");

for(int i=0; i<100; i++) buffer[ i ] = i;

printf("%d %d %d\n", buffer[0], buffer[1], buffer[2]);

asm {
xor AX, AX
mov BX, SEG buffer[0]
mov ES, BX
mov BX, OFFSET buffer[0]
mov result1, [ES:BX]
inc BX
mov result2, [ES:BX]
inc BX
mov result3, [ES:BX]
}

printf("result1 is: %d\nresult2 is: %d\nresult3 is: %d\n", result1, result2, result3);

farfree(buffer);
printf("freed mem.\n");
}

Edited by - Jeremiah on 3/11/00 10:34:14 PM

Share this post


Link to post
Share on other sites
Maybe it''s this:

Your array is an array of unsigned chars, ( which are 1 byte each ) and you are trying to read in an int value ( which is either 2 or 4 bytes depending on the operating system I think ). So maybe you should try to read the values in as char''s rather than integer values. Also I''m pretty sure that using MOV MEMORY,MEMORY is not legal, you may have to MOV REGISTER, MEMORY then MOV MEMORY, REGISTER.

Hmmm, thats all I can think of right now.

Share this post


Link to post
Share on other sites
Dude, you're hosing your AX,ES,and BX registers. You gotta push them before you do anything in an asm block. What you have would work fine in with a straight up assembler, but you have no clue (and probably don't want to have a clue) what the compiler is doing with those registers in this context. So try this:

asm
{
push ax
push bx
push es

blah
blah

pop es
pop bx
pop ax
}

Also, you're probably not gaining anything from that assembly block anyways since the compiler would most likely put your result variables in sequential data spots and use a 'rep movsb' opcode. Its pretty tough to beat a modern compiler, so I really wouldn't recommend throwing around asm blocks anyway.

Edited by - Timdog on 3/12/00 6:22:31 AM

Share this post


Link to post
Share on other sites
Try using ES:DI in stead of ES:BX. This is a much more general way to access memory (and you can use fast instructions like movsx, lodsx and stosx). To load a memory address (of a pointer) in ES:DI easily is:
les di, buffer

Oh, and you're doing:
for (i=0;i<100;i++) buffer=i;
You forget the array subscript. Try this:
for (i=0;i<100;i++) buffer=i;

BTW,
in borland C++ compilers you don't have to push ES, AX and BX. You only have to save/restore DS.

Edited by - bosjoh on 3/12/00 10:35:27 AM

Share this post


Link to post
Share on other sites
I finally fixed the problem... .

another question:
I saw this assembly source code where the guy created a 64K buffer simply by adding a line to the end of his source like...
buffer:
and then:
mov di,offset buffer
and just using that as the beginning of a 64K buffer. why does this work? couldnt he be writing over memory that he isnt supposed to be?

Share this post


Link to post
Share on other sites
Maybe ES was initialized earlier in the code. If it isn''t try to look if he is using the small memory model. If he is, you can only use the first 64K memory using the compiler. Then you don''t need ES (I think).

Correct me if I''m wrong.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!