asm question

Started by
6 comments, last by Jeremiah 24 years, 1 month ago
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"); }
http://fakemind.com
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
Play free Java games at: www.infinitepixels.com
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;<br><br>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.<br><br>I am thinking my assembly is a bit wrong…<br><br>Heres the corrected source (large DOS 16-bit)<br>——<br><br>#include &lt;stdio.h&gt;<br>#include &lt;alloc.h&gt;<br>#include &lt;stdlib.h&gt;<br><br>unsigned char far *buffer;<br><br>void main(void)<br>{<br> int result1 = 0, result2 = 0, result3 = 0;<br> printf("Allocating 64K… ");<br> if((buffer = (unsigned char far *)farmalloc(64000)) == NULL)<br> {<br> printf("Not enough memory\n");<br> exit(1);<br> }<br> printf("done\n");<br><br> for(int i=0; i&lt;100; i++) buffer = i;<br><br> printf("%d %d %d\n", buffer[0], buffer[1], buffer[2]);<br><br> asm {<br> xor AX, AX<br> mov BX, SEG buffer[0]<br> mov ES, BX<br> mov BX, OFFSET buffer[0]<br> mov result1, [ES:BX]<br> inc BX<br> mov result2, [ES:BX]<br> inc BX<br> mov result3, [ES:BX]<br> }<br><br> printf("result1 is: %d\nresult2 is: %d\nresult3 is: %d\n", result1, result2, result3);<br><br> farfree(buffer);<br> printf("freed mem.\n");<br>}<br><br>Edited by - Jeremiah on 3/11/00 10:34:14 PM
http://fakemind.com
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.
Play free Java games at: www.infinitepixels.com
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
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
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?
http://fakemind.com
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.

This topic is closed to new replies.

Advertisement