Article View: pl.comp.lang.asm
Article #2240Re: jeszcze jeszcze prostsza zagadka
From: Andrzej Grażyński
Date: Sat, 08 May 2010 19:25
Date: Sat, 08 May 2010 19:25
85 lines
2628 bytes
2628 bytes
Wojciech Muła pisze:
> Andrzej Grażyński <grazynsk@petex.com.pl> wrote:
>
>> Do rejestru AL wpisać znak odpowiadający szesnastkowej wartości czterech
>> najmniej znaczących bitów.
>>
>> Można to zrobić na przykład tak
>>
>> AND AL,$0F
>> LEA EBX,HEXTABLE
>> XLAT
>>
>> ale można efektywniej: bez odwoływania się do pamięci i bez skoku. Jak?
>
> -- hex.c --
> // kompilacja: gcc hex.c
> #include <stdio.h>
>
> int to_hex(int x) {
> int result;
> asm (
> "andl $0x0f, %%eax \n"
> "leal -10(%%eax), %%ebx \n"
> "shrl $29, %%ebx \n"
> "neg %%ebx \n" // ebx = (eax < 10) ? -7 : 0
> "leal 55(%%eax, %%ebx), %%eax \n"
> : "=a" (result)
> : "a" (x)
> : "ebx"
> );
>
> return result;
> }
>
> int to_hex2(int x) {
> int result;
> asm (
> "andl $0x0f, %%eax \n"
> "leal (0x7fffffff-9)(%%eax), %%ebx \n" // MSB(ebx)=1 gdy eax >= 10
> "sarl $31, %%ebx \n"
> // "andl $7, %%ebx \n" // dla dużych liter (A-F)
> "andl $39, %%ebx \n" // dla małych liter (a-f)
> "leal '0'(%%eax, %%ebx), %%eax \n"
> : "=a" (result)
> : "a" (x)
> : "ebx"
> );
>
> return result;
> }
>
> int main() {
> int i;
> for (i=0; i <= 15; i++)
> printf("%d => %d ('%c')\n", i, to_hex(i), to_hex(i));
>
> puts("");
> for (i=0; i <= 15; i++)
> printf("%d => %d ('%c')\n", i, to_hex2(i), to_hex2(i));
>
> return 0;
> }
> -- eof --
>
> Wersja 2. jest ładniejsza
Kwestia gustu (jeżeli cokolwiek w C może być ładne, wszak to język
ludożerców)
> i przede wszystkim bardziej elastyczna
A na czym ma polegać ta "elastyczność"?
niż nr 1.
>
> w.
Technical Details
Message-ID:
<hs4dr6$g7m$1@news.onet.pl>Path:
mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!not-for-mailReferences:
<hs1uip$ebv$1@news.onet.pl> <20100508210640.741b8634.wojciech_mula@poczta.null.onet.pl.invalid>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-archive.icm.edu.pl!news.rmf.pl!agh.edu.pl!news.agh.edu.pl!news.onet.pl!not-for-mail From: Andrzej Grażyński <grazynsk@petex.com.pl> Newsgroups: pl.comp.lang.asm Subject: Re: jeszcze jeszcze prostsza zagadka Date: Sat, 08 May 2010 21:25:17 +0200 Organization: http://onet.pl Lines: 85 Message-ID: <hs4dr6$g7m$1@news.onet.pl> References: <hs1uip$ebv$1@news.onet.pl> <20100508210640.741b8634.wojciech_mula@poczta.null.onet.pl.invalid> NNTP-Posting-Host: public-gprs66835.centertel.pl Mime-Version: 1.0 X-Trace: news.onet.pl 1273346729 16630 91.94.134.33 (8 May 2010 19:25:29 GMT) X-Complaints-To: niusy@onet.pl NNTP-Posting-Date: Sat, 8 May 2010 19:25:29 +0000 (UTC) User-Agent: Thunderbird 2.0.0.24 (Windows/20100228) In-Reply-To: <20100508210640.741b8634.wojciech_mula@poczta.null.onet.pl.invalid> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit