Article View: pl.comp.lang.asm
Article #2239Re: jeszcze jeszcze prostsza zagadka
From: Wojciech Muła
Date: Sat, 08 May 2010 19:06
Date: Sat, 08 May 2010 19:06
66 lines
2230 bytes
2230 bytes
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 i przede wszystkim bardziej elastyczna niż nr 1.
w.
Technical Details
Message-ID:
<20100508210640.741b8634.wojciech_mula@poczta.null.onet.pl.invalid>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>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: Wojciech Muła <wojciech_mula@poczta.null.onet.pl.invalid> Newsgroups: pl.comp.lang.asm Subject: Re: jeszcze jeszcze prostsza zagadka Date: Sat, 8 May 2010 21:06:40 +0200 Organization: http://onet.pl Lines: 69 Message-ID: <20100508210640.741b8634.wojciech_mula@poczta.null.onet.pl.invalid> References: <hs1uip$ebv$1@news.onet.pl> NNTP-Posting-Host: public-gprs7486.centertel.pl Mime-Version: 1.0 X-Trace: news.onet.pl 1273345625 13882 87.96.29.62 (8 May 2010 19:07:05 GMT) X-Complaints-To: niusy@onet.pl NNTP-Posting-Date: Sat, 8 May 2010 19:07:05 +0000 (UTC) X-Newsreader: Sylpheed 2.5.0 (GTK+ 2.10.14; i686-pc-mingw32) Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit