From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,c9d5fc258548b22a X-Google-NewGroupId: yes X-Google-Attributes: gida07f3367d7,domainid0,public,usenet X-Google-Language: ENGLISH,ASCII Path: g2news2.google.com!postnews.google.com!n2g2000pre.googlegroups.com!not-for-mail From: Syntax Issues Newsgroups: comp.lang.ada Subject: Re: How do I write directly to a memory address? Date: Thu, 3 Feb 2011 02:50:48 -0800 (PST) Organization: http://groups.google.com Message-ID: References: <67063a5b-f588-45ea-bf22-ca4ba0196ee6@l11g2000yqb.googlegroups.com> <31c357bd-c8dc-4583-a454-86d9c579e5f4@m13g2000yqb.googlegroups.com> NNTP-Posting-Host: 65.50.108.230 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1296730248 5026 127.0.0.1 (3 Feb 2011 10:50:48 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 3 Feb 2011 10:50:48 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: n2g2000pre.googlegroups.com; posting-host=65.50.108.230; posting-account=iBd8kwoAAADZsnwFNFLVlwsLLxwDLPUm User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.237 Safari/534.10,gzip(gfe) Xref: g2news2.google.com comp.lang.ada:17813 Date: 2011-02-03T02:50:48-08:00 List-Id: On Feb 3, 3:24=A0am, Ludovic Brenta wrote: > Syntax Issues wrote on comp.lang.ada: > > > > > > > > > > > I am planning to start learning lower-level programming with ada, > > however, I was unable to find a solid tutorial on writing directly to > > a memory address or interfacing with assembly. Does anyone know where > > I can find a reference to some tutorials/information? Below is an > > example of code I would like to be able to implement. > > > ... > > unsigned int print(char *message, unsigned int line) > > { > > =A0 =A0 =A0 =A0 char *vidmem =3D (char *) 0xb8000; > > =A0 =A0 =A0 =A0 unsigned int i=3D 0; > > > =A0 =A0 =A0 =A0 i=3D(line*80*2); > > > =A0 =A0 =A0 =A0 while(*message!=3D0) // 24h > > =A0 =A0 =A0 =A0 { > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 vidmem[i]=3D *message; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 *message++; > > *Every* time I look at C code, I see a bug. > I think this is a bug; it should read "message++" since you want to > increment the pointer, not the pointed-to character. > > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 i++; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 vidmem[i]=3D 0x7; > > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 i++; > > =A0 =A0 =A0 =A0 }; > > > =A0 =A0 =A0 =A0 return(1);}; > > > ... > > The proper way to achieve what you want is not to translate the > (buggy) C code into Ada; instead, use Ada to model the problem and > provide a clean abstraction. =A0In this case, the model is that the > video memory, located at some address, is a two-dimensional array of > cells; each cell contains a character and an attribute byte. > > type Cell_T is record > =A0 =A0Char : Character; > =A0 =A0Attribute : Interfaces.Unsigned_8; > end record; > pragma Pack (Cell_T); > > type Horiz_Coordinate_T is mod 80; > type Vert_Coordinate_T is mod 25; > type Console_T is array > =A0 (Horiz_Coordinate_T, Vert_Coordinate_T) of Cell_T; > pragma Pack (Console_T); > > Console : Console_T; > for Console'Address use 16#b8000#; -- the only low-level trick! > > procedure Put (C =A0 =A0 =A0 =A0 =A0 =A0: in Character; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Into_Console : in out Console_T; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0At_X =A0 =A0 =A0 =A0 : in =A0 =A0 Horiz_Co= ordinate_T; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0At_Y =A0 =A0 =A0 =A0 : in =A0 =A0 Vert_Coo= rdinate_T) is > begin > =A0 =A0Into_Console (At_X, At_Y) :=3D > =A0 =A0 =A0(Char =3D> C, Attribute =3D> 16#7#); > end Put; > > procedure Put (S =A0 =A0 =A0 =A0 =A0 =A0: in =A0 =A0 String; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0Into_Console : in out Console_T; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0At_X =A0 =A0 =A0 =A0 : in =A0 =A0 Horiz_Co= ordinate_T; > =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0At_Y =A0 =A0 =A0 =A0 : in =A0 =A0 Vert_Coo= rdinate_T) is > =A0 X : Horiz_Coordinate_T :=3D At_X; > =A0 Y : Vert_Coordinate_T :=3D At_Y; > begin > =A0 for J in S'Range loop > =A0 =A0 Put (C =3D> S (J), > =A0 =A0 =A0 =A0 =A0Into_Console =3D> Into_Console, > =A0 =A0 =A0 =A0 =A0At_X =3D> X, > =A0 =A0 =A0 =A0 =A0At_Y =3D> Y); > =A0 =A0 X :=3D X + 1; -- modular arithmetic: wraps around > =A0 =A0 if X =3D 0 then > =A0 =A0 =A0 =A0Y :=3D Y + 1; -- ditto > =A0 =A0 end if; > =A0 end loop; > end Put; > > Hope this helps > > -- > Ludovic Brenta. Excellent, this defiantly helps.