Page 1 of 2
How do I move data from a pointer?
Posted: Sun Dec 19, 2010 2:40 pm
by ThatGuy22
I have made a program that makes a pointer to a variable, and then I tried to use a DOS function to print it out, I seem to be doing somthing wrong since it doesn't print out what my variable is. Here is my program:
Code: Select all
mov bx, PointTo ;Make bx point to the variable PointTo
mov ah, 0x0e ;DOS function to print character.
mov al, [bx] ;Print character that bx points to ('H')
int 0x10 ;Print character
PointTo:
db 'H' ;char to print
What may I be doing wrong?
-I think that I may have a problem with moving a value from a pointer stored in a larger register, to the one I am transferring it to that is smaller. This part of my code is on line 3 of my code(mov al, [bx]).
Re: How do I move data from a pointer?
Posted: Sun Dec 19, 2010 2:47 pm
by Combuster
There's no DOS involved.
There are segment registers involved.
Please do not copy and paste code, observe that it doesn't work, then complain to us. Instead observe and learn how the code is achieving its goal.
Re: How do I move data from a pointer?
Posted: Sun Dec 19, 2010 3:24 pm
by Casm
ThatGuy22 wrote:I have made a program that makes a pointer to a variable, and then I tried to use a DOS function to print it out, I seem to be doing somthing wrong since it doesn't print out what my variable is. Here is my program:
Code: Select all
mov bx, PointTo ;Make bx point to the variable PointTo
mov ah, 0x0e ;DOS function to print character.
mov al, [bx] ;Print character that bx points to ('H')
int 0x10 ;Print character
PointTo:
db 'H' ;char to print
What may I be doing wrong?
-I think that I may have a problem with moving a value from a pointer stored in a larger register, to the one I am transferring it to that is smaller. This part of my code is on line 3 of my code(mov al, [bx]).
This probably belongs in the general programming section.
http://www.ctyme.com/intr/rb-0106.htm
After the mov al, [bx] instruction, you need to zero bx. Also you may need:
push cs
pop ds
(The function you are using involves a call to the BIOS, not DOS).
Re: How do I move data from a pointer?
Posted: Sun Dec 19, 2010 5:05 pm
by ThatGuy22
O sorry, and why would I need to zero bx?
From the link "Casm" gave me, I assume my problem is that I can't use bx?
Re: How do I move data from a pointer?
Posted: Sun Dec 19, 2010 5:12 pm
by Casm
ThatGuy22 wrote:O sorry, and why would I need to zero bx?
Because with int 10h, fn 0eh, bh is used to pass the page number and bl the foreground colour. Unless you have changed it previously, the page number will always be zero. Writing to pages 1, 2 or 3 would have no visible effect until you switched to that page.
Even in the early days of the PC, when its graphics capability was, shall we say, nothing to write home about, graphics mode still needed far more video RAM than text mode. So, rather than waste the spare memory when the display was in text mode, there were alternative video text pages you could write to, although only one of them would be displayed at any one time. You switched between those pages using int 10h, fn 5.
Re: How do I move data from a pointer?
Posted: Sun Dec 19, 2010 7:42 pm
by ThatGuy22
Ok, so I tried it using cx for the pointer, but got the error "invalid effective address". Note that I am using nasm to assemble this. My new code is as follows:
Code: Select all
mov cx, PointTo
mov ah, 0x0e
mov al, [cx]
int 0x10
PointTo:
db 'H'
So what may be wrong with this?
Re: How do I move data from a pointer?
Posted: Sun Dec 19, 2010 8:51 pm
by miker00lz
ThatGuy22 wrote:Ok, so I tried it using cx for the pointer, but got the error "invalid effective address". Note that I am using nasm to assemble this. My new code is as follows:
Code: Select all
mov cx, PointTo
mov ah, 0x0e
mov al, [cx]
int 0x10
PointTo:
db 'H'
So what may be wrong with this?
because you dont want [cx] you just want cx. but when you fix that you'll find that you can't move a 16-bit register to an 8-bit register.
do this:
Code: Select all
mov cl, PointTo
mov ah, 0x0e
mov al, cl
int 0x10
PointTo:
db 'H'
the braces in [cx] mean that you're want to move the value of the LOCATION of cx into another register...... being that cx is a REGISTER, it doesnt HAVE a memory location.
and please keep such trivial issues out of "OS Development"?
Re: How do I move data from a pointer?
Posted: Mon Dec 20, 2010 6:23 am
by Gigasoft
mikr00lz wrote:because you dont want [cx] you just want cx. but when you fix that you'll find that you can't move a 16-bit register to an 8-bit register.
the braces in [cx] mean that you're want to move the value of the LOCATION of cx into another register...... being that cx is a REGISTER, it doesnt HAVE a memory location.
and please keep such trivial issues out of "OS Development"?
Oh, please do share more great words of wisdom with us.
On a more serious note, in the x86 family, the possible 16-bit addressing modes are as follows:
[bx+si+displacement]
[bx+di+displacement]
[bp+si+displacement]
[bp+di+displacement]
[si+displacement]
[di+displacement]
[bp+displacement]
[bx+displacement]
[address]
where displacement is a word, signed byte or nothing.
The 32-bit addressing modes are as follows:
[base+displacement]
[base+index+displacement]
[base+index*scale+displacement]
[address+index*scale]
[address]
where base is any register, index is any register except esp, scale is 1, 2, 4 or 8, and displacement is a double word, signed byte or nothing.
[cx] is therefore not a valid addressing mode, but [bx] is valid, and so is [ecx].
By the way, the meaning of mov bx, PointTo is different between assemblers. In NASM, this indicates an immediate operand, which is what you want here. In MASM, it indicates a memory operand, and you would write mov bx, offset PointTo to use the address as an immediate operand.
If you have further questions, please don't hesitate to
RTFM.
Re: How do I move data from a pointer?
Posted: Mon Dec 20, 2010 9:27 am
by ThatGuy22
First off, I thought you can't move pointers into 8 bit registers(cl), that was why I was using cx. Second I wan't the full address to be stored in cx not just the offset or base address. Also I put Breackets('[]') on cx because I do want the value that cx points to.
Re: How do I move data from a pointer?
Posted: Mon Dec 20, 2010 9:46 am
by qw
ThatGuy22,
No offense intended, but this is not the forum to post such questions. You really should learn assembly language elsewhere.
A good tutorial in my opinion is the
Art of Assembly Language Programming. It covers all your questions. It uses MASM syntax which is a little different from NASM syntax, but that shouldn't be a problem. The differences are explained quite well in the
NASM manual.
Re: How do I move data from a pointer?
Posted: Mon Dec 20, 2010 10:25 am
by miker00lz
Gigasoft wrote:mikr00lz wrote:because you dont want [cx] you just want cx. but when you fix that you'll find that you can't move a 16-bit register to an 8-bit register.
the braces in [cx] mean that you're want to move the value of the LOCATION of cx into another register...... being that cx is a REGISTER, it doesnt HAVE a memory location.
and please keep such trivial issues out of "OS Development"?
Oh, please do share more great words of wisdom with us.
On a more serious note, in the x86 family, the possible 16-bit addressing modes are as follows:
[bx+si+displacement]
[bx+di+displacement]
[bp+si+displacement]
[bp+di+displacement]
[si+displacement]
[di+displacement]
[bp+displacement]
[bx+displacement]
[address]
where displacement is a word, signed byte or nothing.
The 32-bit addressing modes are as follows:
[base+displacement]
[base+index+displacement]
[base+index*scale+displacement]
[address+index*scale]
[address]
where base is any register, index is any register except esp, scale is 1, 2, 4 or 8, and displacement is a double word, signed byte or nothing.
[cx] is therefore not a valid addressing mode, but [bx] is valid, and so is [ecx].
By the way, the meaning of mov bx, PointTo is different between assemblers. In NASM, this indicates an immediate operand, which is what you want here. In MASM, it indicates a memory operand, and you would write mov bx, offset PointTo to use the address as an immediate operand.
If you have further questions, please don't hesitate to
RTFM.
i can understand the tone of my post for him makes it easy for somebody to want to tell me off, but as can be seen from the following function in the 8086 emulator i wrote, i understand the addressing modes.
i was keeping it simple for the guy.
Code: Select all
unsigned short getea(unsigned char rmval, unsigned char mod) {
switch (mod) {
case 0:
if (rmval==0) return((*segptr<<4)+bx+si);
else if (rmval==1) return((*segptr<<4)+bx+di);
else if (rmval==2) return((*segptr<<4)+bp+si);
else if (rmval==3) return((*segptr<<4)+bp+di);
else if (rmval==4) return((*segptr<<4)+si);
else if (rmval==5) return((*segptr<<4)+di);
else if (rmval==6) return((*segptr<<4)+disp);
else if (rmval==7) return((*segptr<<4)+bx);
break;
case 1:
if (rmval==0) return((*segptr<<4)+bx+si+disp);
else if (rmval==1) return((*segptr<<4)+bx+di+disp);
else if (rmval==2) return((*segptr<<4)+bp+si+disp);
else if (rmval==3) return((*segptr<<4)+bp+di+disp);
else if (rmval==4) return((*segptr<<4)+si+disp);
else if (rmval==5) return((*segptr<<4)+di+disp);
else if (rmval==6) return((*segptr<<4)+bp+disp);
else if (rmval==7) return((*segptr<<4)+bx+disp);
break;
case 2:
if (rmval==0) return((*segptr<<4)+bx+si+disp);
else if (rmval==1) return((*segptr<<4)+bx+di+disp);
else if (rmval==2) return((*segptr<<4)+bp+si+disp);
else if (rmval==3) return((*segptr<<4)+bp+di+disp);
else if (rmval==4) return((*segptr<<4)+si+disp);
else if (rmval==5) return((*segptr<<4)+di+disp);
else if (rmval==6) return((*segptr<<4)+bp+disp);
else if (rmval==7) return((*segptr<<4)+bx+disp);
break;
case 3: return 0; //should never get to this
}
}
sorry for the somewhat off-topic, but i want to make sure you know i'm not completely retarded.
Re: How do I move data from a pointer?
Posted: Mon Dec 20, 2010 10:33 am
by miker00lz
ThatGuy22 wrote:First off, I thought you can't move pointers into 8 bit registers(cl), that was why I was using cx. Second I wan't the full address to be stored in cx not just the offset or base address. Also I put Breackets('[]') on cx because I do want the value that cx points to.
well, the compiler is actually subtituting "PointTo" for the value of what it's pointing at in your mov statement (although, note it's not statically replacing the value with what you've got at compile-time. the value at PointTo can be changed during execution and then it would use the new value there) so you can mov it to either an 8-bit or 16-bit register. if 8-bit it'll just take the value in the single byte that it points to, if 16-bit that byte and the one that follows it as a word.
so this is why you don't need it here. in the fixed i code i pasted for you, the ASCII value of 'H' is moved into cl. it may seem a bit confusing at first, i know.
Re: How do I move data from a pointer?
Posted: Mon Dec 20, 2010 11:23 am
by Solar
Explaining pointer semantics? On "The Place to Start for Operating System Developers"?
Seriously?
Re: How do I move data from a pointer?
Posted: Mon Dec 20, 2010 12:50 pm
by ThatGuy22
ok, thank you I will find a different forum to post stuff like this, since some people don't want me to post it here - *Cough* *Cough*:
Hobbes wrote:ThatGuy22,
No offense intended, but this is not the forum to post such questions. You really should learn assembly language elsewhere.
A good tutorial in my opinion is the
Art of Assembly Language Programming. It covers all your questions. It uses MASM syntax which is a little different from NASM syntax, but that shouldn't be a problem. The differences are explained quite well in the
NASM manual.
Thanks for the tutorial though.
and *Cough* *Cough*
Solar wrote:Explaining pointer semantics? On "The Place to Start for Operating System Developers"?
Seriously?
Re: How do I move data from a pointer?
Posted: Mon Dec 20, 2010 7:12 pm
by miker00lz
berkus wrote:Solar wrote:Explaining pointer semantics? On "The Place to Start for Operating System Developers"?
Seriously?
YA RLY. And also explaining some assembler basics to miker00lz. Dude, find the mistake in your code below (NASM syntax):
miker00lz wrote:Code: Select all
mov cl, PointTo
mov ah, 0x0e
mov al, cl
int 0x10
PointTo:
db 'H'
i don't use NASM. this compiled in emu8086. but now that you mention it that should be PointTo db 'H' without the colon. i wasn't sure if that worked in NASM or not. it won't show an H in emu8086, but it compiled. wasn't sure if NASM would do that or not. and i'm not trying to say i'm an assembly god.