* Newbie Question
@ 2002-10-24 21:42 dAvId KeDrOsKy
2002-10-24 22:15 ` Rudolf Marek
2002-10-24 23:43 ` Brian Raiter
0 siblings, 2 replies; 16+ messages in thread
From: dAvId KeDrOsKy @ 2002-10-24 21:42 UTC (permalink / raw)
To: Linux Assembly Programming List
Hello,
I have recently started learning x86 assembly and have been programming it in
Linux with NASM (Netwide Assembler).
I am trying to write a program that takes a string as input, reverses it and
calculated its length. I'm using the stack to do so.
Here's my problem.. NASM only lets me move the string into a 32-bit register.
e.g. mov EBX, the_string ; move memory address of the_string into EBX
At this point I need to push the register on the stack. I know I can do:
push EBX
call string_length_procedure
BUT I was wondering how it is possible to push BX instead. Does BX also hold
the string in this case, considering the size of the string it isn't that
big? If not, is there a way to put the contents of EBX (the string) into BX?
For the time being I would like to just deal with 16-bit registers because it
is easier to follow along with the book I am using.
P.s. I am using an already coded procedure that gets the string for me and
puts it into 'string' in this case.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
2002-10-24 21:42 dAvId KeDrOsKy
@ 2002-10-24 22:15 ` Rudolf Marek
2002-10-24 23:43 ` Brian Raiter
1 sibling, 0 replies; 16+ messages in thread
From: Rudolf Marek @ 2002-10-24 22:15 UTC (permalink / raw)
To: dAvId KeDrOsKy; +Cc: Linux Assembly Programming List
> BUT I was wondering how it is possible to push BX instead. Does BX also hold
> the string in this case, considering the size of the string it isn't that
> big? If not, is there a way to put the contents of EBX (the string) into BX?
> For the time being I would like to just deal with 16-bit registers because it
> is easier to follow along with the book I am using.
Yes but if you are using linux it uses 32-bit memory pointers so you
should use the 32 bit regs. If you want use some "done" work which is
16bit you can just put "e" in front of 16 bit registers to ensure
functionality. (I think)
Regards
Rudolf
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
2002-10-24 21:42 dAvId KeDrOsKy
2002-10-24 22:15 ` Rudolf Marek
@ 2002-10-24 23:43 ` Brian Raiter
2002-10-25 3:07 ` Robin Miyagi
1 sibling, 1 reply; 16+ messages in thread
From: Brian Raiter @ 2002-10-24 23:43 UTC (permalink / raw)
To: linux-assembly
> Here's my problem.. NASM only lets me move the string into a 32-bit
> register.
Please note that the register does NOT contain the string -- just a
POINTER to the string (i.e., its memory address).
> BUT I was wondering how it is possible to push BX instead. Does BX
> also hold the string in this case, considering the size of the
> string it isn't that big?
The size of the POINTER has no relationship to the size of the
string. (If I give you a street address, like 1148 23rd Ave. S, you
have no way of telling from that address if the house is big or
small.)
> If not, is there a way to put the contents of EBX (the string) into
> BX? For the time being I would like to just deal with 16-bit
> registers because it is easier to follow along with the book I am
> using.
Then perhaps you need a different book.
Imagine an airplane pilot climbing into the cockpit of a 747 and
asking how he can use a propeller instead of a jet engine, because he
doesn't need to fly very far, and the flying instructions he was given
was for an airplane with a propeller. What odds would you give that
that pilot is going to be able to get to the runway, much less off of
it?
b
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
2002-10-24 23:43 ` Brian Raiter
@ 2002-10-25 3:07 ` Robin Miyagi
0 siblings, 0 replies; 16+ messages in thread
From: Robin Miyagi @ 2002-10-25 3:07 UTC (permalink / raw)
To: linux-assembly
Here is a 32 bit i386 assembler course;
http://www.drpaulcarter.com/pcasm/
It has good linux coverage. This link can be found on Konstanin's
linux assembly resources page.
On Thursday 24 October 2002 16:43, Brian Raiter wrote:
>
> Then perhaps you need a different book.
>
--
Robin Miyagi<penguin-computing@c2c2c.ca>
http://penguin.c2c2c.ca/
The funny thing about brakes, is that when they break, they don't brake.
^ permalink raw reply [flat|nested] 16+ messages in thread
* Newbie Question
@ 2003-02-27 22:57 dAvId KeDrOsKy
2003-02-27 23:19 ` Dragan Stancevic
` (3 more replies)
0 siblings, 4 replies; 16+ messages in thread
From: dAvId KeDrOsKy @ 2003-02-27 22:57 UTC (permalink / raw)
To: Linux Assembly Programming List
The following program gives me a seg fault. But I don't understand why.
1. mov ebx, dword 1234
2. mov ax, [ebx]
To me the code would mean 1) move the integer 1234 into ebx and then 2) move
the value of ebx into ax. But of course my reasoning is incorrect.
Feedback would be great.
Dave
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
2003-02-27 22:57 dAvId KeDrOsKy
@ 2003-02-27 23:19 ` Dragan Stancevic
2003-02-27 23:26 ` Rudolf Marek
` (2 subsequent siblings)
3 siblings, 0 replies; 16+ messages in thread
From: Dragan Stancevic @ 2003-02-27 23:19 UTC (permalink / raw)
To: dAvId KeDrOsKy, Linux Assembly Programming List
On Thursday 27 February 2003 14:57, dAvId KeDrOsKy wrote:
> The following program gives me a seg fault. But I don't understand why.
>
> 1. mov ebx, dword 1234
> 2. mov ax, [ebx]
>
> To me the code would mean 1) move the integer 1234 into ebx and then 2)
> move the value of ebx into ax. But of course my reasoning is incorrect.
You are trying to apply indirection to ebx, failing while trying to read from
a memory location that is not accessible to your process.
Drop the brackets, and also you probably want eax, right?
--
Peace can only come as a natural consequence
of universal enlightenment. -Dr. Nikola Tesla
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
2003-02-27 22:57 dAvId KeDrOsKy
2003-02-27 23:19 ` Dragan Stancevic
@ 2003-02-27 23:26 ` Rudolf Marek
2003-02-27 23:42 ` Robert G. Plantz
2003-02-28 0:27 ` Slack Traq
3 siblings, 0 replies; 16+ messages in thread
From: Rudolf Marek @ 2003-02-27 23:26 UTC (permalink / raw)
To: dAvId KeDrOsKy; +Cc: Linux Assembly Programming List
>
> The following program gives me a seg fault. But I don't understand why.
>
> 1. mov ebx, dword 1234
> 2. mov ax, [ebx]
>
> To me the code would mean 1) move the integer 1234 into ebx and then
its OK
2) move the value of ebx into ax. But of course my reasoning is incorrect.
BAD 1) you cant use opernads of different sizes.
on one side must be 32 on another 32 tooo
2) operator [ ] is used for referencing into memory,
so you were accessing a memory and thats why the segfault
(adress 0x1234 of process is onexistent)
if you want to "shorter" 32 bit value to 16 bit then use
mov eax,0 ;xor eax,eax works too
mov ax,bx ;copy value from BX to AX
Ah dragan you were faster....
Regards
Rudolf
>
> Feedback would be great.
>
> Dave
> -
> To unsubscribe from this list: send the line "unsubscribe linux-assembly" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
>
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
2003-02-27 22:57 dAvId KeDrOsKy
2003-02-27 23:19 ` Dragan Stancevic
2003-02-27 23:26 ` Rudolf Marek
@ 2003-02-27 23:42 ` Robert G. Plantz
2003-02-28 6:28 ` hpr
2003-02-28 0:27 ` Slack Traq
3 siblings, 1 reply; 16+ messages in thread
From: Robert G. Plantz @ 2003-02-27 23:42 UTC (permalink / raw)
To: dAvId KeDrOsKy; +Cc: Linux Assembly Programming List
On Thursday, February 27, 2003, at 02:57 PM, dAvId KeDrOsKy wrote:
> The following program gives me a seg fault. But I don't understand
> why.
>
> 1. mov ebx, dword 1234
> 2. mov ax, [ebx]
>
> To me the code would mean 1) move the integer 1234 into ebx and then
> 2) move
> the value of ebx into ax. But of course my reasoning is incorrect.
Assuming that you want the (16-bit) word at address 1234 to be loaded
into ax, I see two problems:
a. address 1234 probably cannot be accessed by your program, and
b. linux is unhappy if you change ebx. (Your program needs to
save/restore ebx.)
Bob
------------------------------------------------------------------------
---------------
Robert G. Plantz, PhD | http://www.cs.sonoma.edu/~bob
Sonoma State University | ph: (707) 664-2806
CS Department | fax: (707) 664-3012
1801 E. Cotati Avenue | email: plantz@sonoma.edu
Rohnert Park, CA 94928
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
2003-02-27 22:57 dAvId KeDrOsKy
` (2 preceding siblings ...)
2003-02-27 23:42 ` Robert G. Plantz
@ 2003-02-28 0:27 ` Slack Traq
3 siblings, 0 replies; 16+ messages in thread
From: Slack Traq @ 2003-02-28 0:27 UTC (permalink / raw)
To: dAvId KeDrOsKy; +Cc: Linux Assembly Mailing List
--- dAvId KeDrOsKy <davidkedrosky@rogers.com> wrote:
> The following program gives me a seg fault. But I
> don't understand why.
>
> 1. mov ebx, dword 1234
> 2. mov ax, [ebx]
>
> To me the code would mean 1) move the integer 1234
> into ebx and then 2) move
> the value of ebx into ax. But of course my
> reasoning is incorrect.
First of all, if you need to pass an integer to ebx
(32 bit register) you may wanna to do
1. mov ebx, 1234
Second, if you put the brackets between a register,
its memory address is passed to ax, in this case.
Third, you can't move a 32 bit register into a 16 bit
one. Use eax, instead.
2. mov eax, ebx
Advice: you can decompose a 32 bit register, ie eax,
in ax. Then you can decompose ax in ah and al (8 bit
registers).
If hope it will help you.
>
> Feedback would be great.
>
> Dave
> -
> To unsubscribe from this list: send the line
> "unsubscribe linux-assembly" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at
http://vger.kernel.org/majordomo-info.html
Regards,
Slack
__________________________________________________
Do you Yahoo!?
Yahoo! Tax Center - forms, calculators, tips, more
http://taxes.yahoo.com/
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
[not found] <Pine.VMS.3.91-2(vms).1030228001923.25337A-100000@cs.felk.cvut.cz>
@ 2003-02-28 0:45 ` Dragan Stancevic
0 siblings, 0 replies; 16+ messages in thread
From: Dragan Stancevic @ 2003-02-28 0:45 UTC (permalink / raw)
To: Rudolf Marek, dAvId KeDrOsKy; +Cc: Linux Assembly Programming List
On Thursday 27 February 2003 15:26, Rudolf Marek wrote:
> Ah dragan you were faster....
> Regards
:-))))))
--
Peace can only come as a natural consequence
of universal enlightenment. -Dr. Nikola Tesla
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Newbie Question
2003-02-27 23:42 ` Robert G. Plantz
@ 2003-02-28 6:28 ` hpr
0 siblings, 0 replies; 16+ messages in thread
From: hpr @ 2003-02-28 6:28 UTC (permalink / raw)
To: Robert G. Plantz, dAvId KeDrOsKy; +Cc: Linux Assembly Programming List
Robert G. Plantz am Donnerstag, 27. Februar 2003 23:42:
> Assuming that you want the (16-bit) word at address 1234 to be loaded
> into ax, I see two problems:
> a. address 1234 probably cannot be accessed by your program, and
> b. linux is unhappy if you change ebx. (Your program needs to
> save/restore ebx.)
not linux, but (probably) "C"...
i/o related syscalls use ebx for the file descriptor
hp
--
Linux,Assembly,Forth: <http://www.lxhp.in-berlin.de/index-lx.shtml>
pse, reply to << lxhp -at- lxhp -dot- in-berlin -dot- de >>
^ permalink raw reply [flat|nested] 16+ messages in thread
* newbie question
@ 2005-09-03 17:16 raphael
2005-09-03 17:36 ` jeff
0 siblings, 1 reply; 16+ messages in thread
From: raphael @ 2005-09-03 17:16 UTC (permalink / raw)
To: linux-assembly
Hello list
I would like to know how i can manage to read the entry
on the keyboard without using C functions like scanf or get.
thank you for the help and forgive my poor english
Raphael
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: newbie question
2005-09-03 17:16 newbie question raphael
@ 2005-09-03 17:36 ` jeff
2005-09-03 20:10 ` Martin Fflores
2005-09-03 20:10 ` Reading at port 60h Martin Fflores
0 siblings, 2 replies; 16+ messages in thread
From: jeff @ 2005-09-03 17:36 UTC (permalink / raw)
Cc: linux-assembly
On 09/03/2005 10:16 am, raphael wrote:
> I would like to know how i can manage to read the entry
> on the keyboard without using C functions like scanf or get.
You can use kernel calls. The keyboard operates in two
modes and you need to select the mode first.
In raw mode each key press is provided. In cooked mode
a whole line of data is provided when <enter> key pressed.
There are lots of examples, but the code looks like this:
1. select mode, save previous mode
2. wait for key press
3. restore mode
Here is some code from asmlib. It shows handling of multi
byte keys and the mouse. This is probably does more than
you want, but may be useful.
[section .text]
extern raw_set2
extern raw_unset2
extern kbuf
extern key_poll
extern mouse_check
extern poll_rtn
extern kbuf_end
extern key_flush
; read_stdin - read stdin data to "kbuf"
; INPUTS
; none
; OUTPUT
; kbuf - contains keys read with zero termination
; NOTES
; source file: read_stdin.asm
;
; read_stdin calls raw_set2 to put keyboard in raw
; mode. It then reads data to "kbuf" and calls
; "raw_unset2 to restore terminal state.
;
global read_stdin
read_stdin:
call raw_set2 ;set raw mode and save previous mode
km_10:
mov ecx,kbuf
more_keys:
mov eax,3 ;kernel read call
mov edx,1 ;read one byte
mov ebx,0 ;read stdin
int 80h
or eax,eax
js km_15
add ecx,eax ;advance buffer ptr
call key_poll
test byte [poll_rtn],8 ;check for error
jz km_20
km_15:
call key_flush
jmp short km_10
km_20:
cmp ecx,kbuf_end
je k_exit ;exit if buffer full
test byte [poll_rtn],1
jnz more_keys
k_exit:
mov byte [ecx],0 ;terminate string
call mouse_check ;check if mouse data read
call raw_unset2
ret
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: newbie question
2005-09-03 17:36 ` jeff
@ 2005-09-03 20:10 ` Martin Fflores
2005-09-03 20:10 ` Reading at port 60h Martin Fflores
1 sibling, 0 replies; 16+ messages in thread
From: Martin Fflores @ 2005-09-03 20:10 UTC (permalink / raw)
To: linux-assembly
hi people, how i can have noticie that a key was pressed , but reading at
port 60h, my problem is that i cant find a good documentation, if some one
have some sourcecode(in ATT Assembly)Documentation or web link that will be
great.
Take Folks.
_________________________________________________________________
Don’t just search. Find. Check out the new MSN Search!
http://search.msn.click-url.com/go/onm00200636ave/direct/01/
^ permalink raw reply [flat|nested] 16+ messages in thread
* Reading at port 60h
2005-09-03 17:36 ` jeff
2005-09-03 20:10 ` Martin Fflores
@ 2005-09-03 20:10 ` Martin Fflores
2005-09-04 2:56 ` Richard Cooper
1 sibling, 1 reply; 16+ messages in thread
From: Martin Fflores @ 2005-09-03 20:10 UTC (permalink / raw)
To: linux-assembly
hi people, how i can have noticie that a key was pressed , but reading at
port 60h, my problem is that i cant find a good documentation, if some one
have some sourcecode(in ATT Assembly)Documentation or web link that will be
great.
Take Folks.
_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar - get it now!
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: Reading at port 60h
2005-09-03 20:10 ` Reading at port 60h Martin Fflores
@ 2005-09-04 2:56 ` Richard Cooper
0 siblings, 0 replies; 16+ messages in thread
From: Richard Cooper @ 2005-09-04 2:56 UTC (permalink / raw)
To: Martin Fflores; +Cc: linux-assembly
> how i can have noticie that a key was pressed , but reading at port 60h,
You aren't doing that in Linux are you? Linux reads the keyboard via an
interrupt, if you're polling through port 0x60/0x64, you're not going to
get anything because the kernel will get to it first.
You can have the kernel give you the scancodes from the keyboard on your
stdin by using ioctls. You can get more information about that by typing
"man console_ioctl"
Basically, you call KDGKBMODE to get the current mode and save it, then
call KDSKBMODE to set "RAW" mode, and when your program exits, you set
the keyboard mode back like it was. However, before that, you have to
install signal handlers to catch if your program crashes or someone tries
to kill it, because if your program doesn't set the keyboard mode back,
the kernel isn't going to do it. Also, before calling KDSKBMODE, you need
to call TCSETSW to change the TTY settings, otherwise scancode 3 will be
interpreted as contol-c, and scancode 13 will be translated to 10, and 127
will be translated to 8, and probably some more. You also have to use
TCGETS to save those settings, so you can restore them too. Then you have
to watch for Alt-Fn and signal the kernel via VT_ACTIVATE to switch
consoles, because it doesn't watch for Alt-Fn when they keyboard is in RAW
mode. On the plus side, you don't have to switch back to non-raw mode
when you call VT_ACTIVATE, because the kernel keeps each console in it's
own mode.
It's a mess of work, but it's doable, and will certainly work better than
direct access.
> my problem is that i cant find a good documentation,
I've got a book, The Indispensable PC Hardware Book, which is a pretty
good book. It tells how to do direct hardware access with most things
that it covers (but not much on VGA, unfortunately), and most of the
examples are in assembly language. The keyboard chapter is a pain to
read, and I never did anything sucessfully with it, but then last time I
tried was 10 years ago when I was 16, so maybe I just wasn't smart
enough. However, I didn't have any trouble with doing direct IDE access
after reading the chapter about that, so I don't know, maybe there's just
something wrong with that chapter.
Looking it over, it is quite confusing (e.g., the "output buffer" is the
port that you read scancodes from, and the "input port" isn't a port at
all), but it seems like the information is all there.
Looks like the keyboard I/O is definately confusing, though. You've got
the two ports you communicate to the controller with, then the controller
has two ports that it communicates to the keyboard with, as well as
setting things like the A20 gate, and then the keyboard is what does
stuff. Along the way there's an "output port" (of which the A20 gate is a
part of) which the controller can both write to and read from, but to
access it, you have to tell the controller to access it, because it's the
controller's port, not yours.
> web link that will be great.
Normally the best documentation you can find on the web is the datasheet
for the component itself:
http://www.jameco.com/webapp/wcs/stores/servlet/ProductDisplay?langId=-1&storeId=10001&catalogId=10001&productId=53014
However, the keyboard controller is just a generic programmable
computer-in-a-chip. So the datasheet doesn't help much.
Book link:
http://www.amazon.com/exec/obidos/tg/detail/-/0201596164/qid=1125801989/sr=8-1/ref=pd_bbs_1/102-3152860-8785751?v=glance&s=books&n=507846
^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2005-09-04 2:56 UTC | newest]
Thread overview: 16+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-09-03 17:16 newbie question raphael
2005-09-03 17:36 ` jeff
2005-09-03 20:10 ` Martin Fflores
2005-09-03 20:10 ` Reading at port 60h Martin Fflores
2005-09-04 2:56 ` Richard Cooper
[not found] <Pine.VMS.3.91-2(vms).1030228001923.25337A-100000@cs.felk.cvut.cz>
2003-02-28 0:45 ` Newbie Question Dragan Stancevic
-- strict thread matches above, loose matches on Subject: below --
2003-02-27 22:57 dAvId KeDrOsKy
2003-02-27 23:19 ` Dragan Stancevic
2003-02-27 23:26 ` Rudolf Marek
2003-02-27 23:42 ` Robert G. Plantz
2003-02-28 6:28 ` hpr
2003-02-28 0:27 ` Slack Traq
2002-10-24 21:42 dAvId KeDrOsKy
2002-10-24 22:15 ` Rudolf Marek
2002-10-24 23:43 ` Brian Raiter
2002-10-25 3:07 ` Robin Miyagi
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).