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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham 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!m13g2000yqb.googlegroups.com!not-for-mail From: Ludovic Brenta Newsgroups: comp.lang.ada Subject: Re: How do I write directly to a memory address? Date: Thu, 3 Feb 2011 00:24:38 -0800 (PST) Organization: http://groups.google.com Message-ID: <31c357bd-c8dc-4583-a454-86d9c579e5f4@m13g2000yqb.googlegroups.com> References: <67063a5b-f588-45ea-bf22-ca4ba0196ee6@l11g2000yqb.googlegroups.com> NNTP-Posting-Host: 153.98.68.197 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1296721478 31735 127.0.0.1 (3 Feb 2011 08:24:38 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Thu, 3 Feb 2011 08:24:38 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: m13g2000yqb.googlegroups.com; posting-host=153.98.68.197; posting-account=pcLQNgkAAAD9TrXkhkIgiY6-MDtJjIlC User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:17808 Date: 2011-02-03T00:24:38-08:00 List-Id: 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. In 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 Char : Character; Attribute : 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 (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 : in Character; Into_Console : in out Console_T; At_X : in Horiz_Coordinate_T; At_Y : in Vert_Coordinate_T) is begin Into_Console (At_X, At_Y) :=3D (Char =3D> C, Attribute =3D> 16#7#); end Put; procedure Put (S : in String; Into_Console : in out Console_T; At_X : in Horiz_Coordinate_T; At_Y : in Vert_Coordinate_T) is X : Horiz_Coordinate_T :=3D At_X; Y : Vert_Coordinate_T :=3D At_Y; begin for J in S'Range loop Put (C =3D> S (J), Into_Console =3D> Into_Console, At_X =3D> X, At_Y =3D> Y); X :=3D X + 1; -- modular arithmetic: wraps around if X =3D 0 then Y :=3D Y + 1; -- ditto end if; end loop; end Put; Hope this helps -- Ludovic Brenta.