Re: Question about jumps
- From: Herbert Kleebauer <klee@xxxxxxxxx>
- Date: Wed, 30 Aug 2006 19:59:21 +0200
Evenbit wrote:
...and then you add some more until you run into code that doesn't
work...
I don't understand the purpose of the macros. If they are
only a few instructions, then write them directly into the
code (makes the code better readable). And if they are many
instructions, write it as a subroutine and not as a macro. The
same program in a readable version without any macros:
;===========================================================================
seg32
@=$08048000
code_offset=@@
code_addr:
;--------------------------- ELF header -----------------------------------
dc.l $464c457f,$00010101,0,0,$00030002,1,main,$34,0,0,$00200034,2,0
dc.l 1,code_offset,code_addr,code_addr,code_filez,code_memsz,5,4096
dc.l 1,data_offset,data_addr,data_addr,data_filez,data_memsz,6,4096
;--------------------------- code ------------------------------------------
main: move.l #text1,r2
move.l #text1_l,r1
bsr.l out_text
move.l #buf,r2
move.b #'3',(r2)
move.l #1,r1
bsr.l out_text_lf
move.b #'4',1.b(r2)
move.l #2,r1
bsr.l out_text_lf
move.b #42,r0
move.l #buf,r1
move.l r1,r2
bsr.l out_hex_b
sub.l r2,r1
bsr.l out_text_lf
move.l #text2,r2
move.l #text2_l,r1
bsr.l out_text
move.l #0,r3 ; return code
move.l #1,r0 ; exit
trap #$80
out_text:
movem.l r0-r7,-(sp)
move.l #1,r3 ; stdout
move.l #4,r0 ; write
trap #$80
movem.l (sp)+,r0-r7
rts.l
out_text_lf:
movem.l r0-r7,-(sp)
move.l #1,r3 ; stdout
move.l #4,r0 ; write
trap #$80
move.l #text0,r2
move.l #1,r1
move.l #1,r3 ; stdout
move.l #4,r0 ; write
trap #$80
movem.l (sp)+,r0-r7
rts.l
out_hex_l:
ror.l #16,r0
bsr.l out_hex_w
ror.l #16,r0
out_hex_w:
ror.w #8,r0
bsr.l out_hex_b
ror.w #8,r0
out_hex_b:
ror.b #4,r0
bsr.l out_hex_n
ror.b #4,r0
out_hex_n:
move.l r0,-(sp)
andq.l #$0f,r0
cmp.b #9,r0
bls.b _10
add.b #'a'-'0'-10,r0
_10: add.b #'0',r0
move.b r0,(r1)
addq.l #1,r1
move.l (sp)+,r0
rts.l
;--------------------------- constant data ---------------------------------
text0: dc.b 10
text1: dc.b "Hello, world!",10
text1_l=@-text1
text2: dc.b "Goodbye, world!",10
text2_l=@-text2
;---------------------------------------------------------------------------
code_filez=@@-code_offset
code_memsz= @-code_addr
even 4
@=(@+4095)/4096*4096+(@@\4096)
data_offset=@@
data_addr:
;--------------------------- initialized data ------------------------------
;--------------------------- uninitialized data ----------------------------
buf: blk.b 64
;---------------------------------------------------------------------------
data_filez=@@-data_offset
data_memsz= @-data_addr
;===========================================================================
Any of the 352 bytes in the output file (how big is your output file?)
is explicitly specified in the source code. No cryptic command line for
the assembler and linker necessary like:
; nasm -f elf -l stk3.lst -Xvc -O8 -o stk3.o stk3.asm
; ld -s -o stk3 stk3.o
Now, compare the readability of the above code with your code:
; define some system constants
_sys_exit equ 1
_sys_write equ 4
_sys_stdout equ 2
; define some system macros
%macro sys_exit 1
mov eax, _sys_exit
mov ebx, %1
int 0x80
%endmacro
%macro writestr 2+
[section .data]
%%str: db %2
%%endstr:
__SECT__
mov ecx, %%str
mov edx, %%endstr-%%str
mov ebx, %1
mov eax, _sys_write
int 0x80
%endmacro
%macro nl 0
mov ecx, lf
mov edx, 1
mov ebx, _sys_stdout
mov eax, _sys_write
int 0x80
%endmacro
%macro writebuff 2
mov edx, %2
mov ecx, buff
mov ebx, %1
mov eax, _sys_write
int 0x80
%endmacro
global _start
section .data
hw db 'Hello, World!', 10
hwlen equ $-hw
lf db 10
section .bss
buff: times 64 resb 0
section .text
_start:
mov edx, hwlen
mov ecx, hw
mov ebx, _sys_stdout
mov eax, _sys_write
int 0x80
mov al, '3'
mov [buff], al
mov edx, 1
lea ecx, [buff]
mov ebx, _sys_stdout
mov eax, _sys_write
int 0x80
nl
mov al, '4'
mov [buff+1], al
writebuff _sys_stdout, 2
nl
mov dl, 42
call write_hex
nl
writestr _sys_stdout, 'Goodbye, World!', 10
sys_exit 0
write_hex:
push cx
push dx
mov dh, dl
mov cx, 4
shr dl, cl
call write_hex_digit
mov dl, dh
and dl, 0fh
call write_hex_digit
pop dx
pop cx
ret
write_hex_digit:
push dx
cmp dl, 10
jae .hex_letter
add dl, 48
jmp .write_digit
.hex_letter:
add dl, 55
.write_digit:
call write_char
pop dx
ret
write_char:
push eax
push ecx
push edx
push ebx
mov [buff], dl
mov edx, 1
mov ecx, [buff]
mov ebx, _sys_stdout
mov eax, _sys_write
int 0x80
pop ebx
pop edx
pop ecx
pop eax
ret
...then you scratch your nawgin ;)
Nathan.
And here the listing which shows you any of the 352 bytes in the executable:
;===========================================================================
seg32
@=$08048000
code_offset=@@
code_addr:
;--------------------------- ELF header -----------------------------------
00000000: 08048000: 464c457f 00010101
00000008: 08048008: 00000000 00000000
00000010: 08048010: 00030002 00000001
00000018: 08048018: 08048074 00000034
00000020: 08048020: 00000000 00000000
00000028: 08048028: 00200034 00000002
00000030: 08048030: 00000000 dc.l $464c457f,$00010101,0,0,$00030002,1,main,$34,0,0,$00200034,2,0
00000034: 08048034: 00000001 00000000
0000003c: 0804803c: 08048000 08048000
00000044: 08048044: 0000015e 0000015e
0000004c: 0804804c: 00000005 00001000 dc.l 1,code_offset,code_addr,code_addr,code_filez,code_memsz,5,4096
00000054: 08048054: 00000001 00000160
0000005c: 0804805c: 08049160 08049160
00000064: 08048064: 00000000 00000040
0000006c: 0804806c: 00000006 00001000 dc.l 1,data_offset,data_addr,data_addr,data_filez,data_memsz,6,4096
;--------------------------- code ------------------------------------------
00000074: 08048074: b9 08048140 main: move.l #text1,r2
00000079: 08048079: ba 0000000e move.l #text1_l,r1
0000007e: 0804807e: e8 00000050 bsr.l out_text
00000083: 08048083: b9 08049160 move.l #buf,r2
00000088: 08048088: c6 01 33 move.b #'3',(r2)
0000008b: 0804808b: ba 00000001 move.l #1,r1
00000090: 08048090: e8 0000004e bsr.l out_text_lf
00000095: 08048095: c6 41 01 34 move.b #'4',1.b(r2)
00000099: 08048099: ba 00000002 move.l #2,r1
0000009e: 0804809e: e8 00000040 bsr.l out_text_lf
000000a3: 080480a3: b0 2a move.b #42,r0
000000a5: 080480a5: ba 08049160 move.l #buf,r1
000000aa: 080480aa: 89 d1 move.l r1,r2
000000ac: 080480ac: e8 00000070 bsr.l out_hex_b
000000b1: 080480b1: 29 ca sub.l r2,r1
000000b3: 080480b3: e8 0000002b bsr.l out_text_lf
000000b8: 080480b8: b9 0804814e move.l #text2,r2
000000bd: 080480bd: ba 00000010 move.l #text2_l,r1
000000c2: 080480c2: e8 0000000c bsr.l out_text
000000c7: 080480c7: bb 00000000 move.l #0,r3 ; return code
000000cc: 080480cc: b8 00000001 move.l #1,r0 ; exit
000000d1: 080480d1: cd 80 trap #$80
out_text:
000000d3: 080480d3: 60 movem.l r0-r7,-(sp)
000000d4: 080480d4: bb 00000001 move.l #1,r3 ; stdout
000000d9: 080480d9: b8 00000004 move.l #4,r0 ; write
000000de: 080480de: cd 80 trap #$80
000000e0: 080480e0: 61 90 movem.l (sp)+,r0-r7
000000e2: 080480e2: c3 rts.l
out_text_lf:
000000e3: 080480e3: 60 movem.l r0-r7,-(sp)
000000e4: 080480e4: bb 00000001 move.l #1,r3 ; stdout
000000e9: 080480e9: b8 00000004 move.l #4,r0 ; write
000000ee: 080480ee: cd 80 trap #$80
000000f0: 080480f0: b9 0804813f move.l #text0,r2
000000f5: 080480f5: ba 00000001 move.l #1,r1
000000fa: 080480fa: bb 00000001 move.l #1,r3 ; stdout
000000ff: 080480ff: b8 00000004 move.l #4,r0 ; write
00000104: 08048104: cd 80 trap #$80
00000106: 08048106: 61 90 movem.l (sp)+,r0-r7
00000108: 08048108: c3 rts.l
out_hex_l:
00000109: 08048109: c1 c8 10 ror.l #16,r0
0000010c: 0804810c: e8 00000003 bsr.l out_hex_w
00000111: 08048111: c1 c8 10 ror.l #16,r0
out_hex_w:
00000114: 08048114: 66 c1 c8 08 ror.w #8,r0
00000118: 08048118: e8 00000004 bsr.l out_hex_b
0000011d: 0804811d: 66 c1 c8 08 ror.w #8,r0
out_hex_b:
00000121: 08048121: c0 c8 04 ror.b #4,r0
00000124: 08048124: e8 00000003 bsr.l out_hex_n
00000129: 08048129: c0 c8 04 ror.b #4,r0
out_hex_n:
0000012c: 0804812c: 50 move.l r0,-(sp)
0000012d: 0804812d: 83 e0 0f andq.l #$0f,r0
00000130: 08048130: 3c 09 cmp.b #9,r0
00000132: 08048132: 76 02 bls.b _10
00000134: 08048134: 04 27 add.b #'a'-'0'-10,r0
00000136: 08048136: 04 30 _10: add.b #'0',r0
00000138: 08048138: 88 02 move.b r0,(r1)
0000013a: 0804813a: 83 c2 01 addq.l #1,r1
0000013d: 0804813d: 58 move.l (sp)+,r0
0000013e: 0804813e: c3 rts.l
;--------------------------- constant data ---------------------------------
0000013f: 0804813f: 0a text0: dc.b 10
00000140: 08048140: 48 65 6c 6c 6f 2c
00000146: 08048146: 20 77 6f 72 6c 64
0000014c: 0804814c: 21 0a text1: dc.b "Hello, world!",10
text1_l=@-text1
0000014e: 0804814e: 47 6f 6f 64 62 79
00000154: 08048154: 65 2c 20 77 6f 72
0000015a: 0804815a: 6c 64 21 0a text2: dc.b "Goodbye, world!",10
text2_l=@-text2
;---------------------------------------------------------------------------
code_filez=@@-code_offset
code_memsz= @-code_addr
0000015e: 0804815e: 00 00 even 4
@=(@+4095)/4096*4096+(@@\4096)
data_offset=@@
data_addr:
;--------------------------- initialized data ------------------------------
;--------------------------- uninitialized data ----------------------------
buf: blk.b 64
;---------------------------------------------------------------------------
data_filez=@@-data_offset
data_memsz= @-data_addr
;===========================================================================
code_offset.... 00000000 code_addr...... 08048000 main........... 08048074
out_text....... 080480d3 out_text_lf.... 080480e3 out_hex_l...... 08048109
out_hex_w...... 08048114 out_hex_b...... 08048121 out_hex_n...... 0804812c
text0.......... 0804813f text1.......... 08048140 text1_l........ 0000000e
text2.......... 0804814e text2_l........ 00000010 code_filez..... 0000015e
code_memsz..... 0000015e data_offset.... 00000160 data_addr...... 08049160
buf............ 08049160 data_filez..... 00000000 data_memsz..... 00000040
.
- Follow-Ups:
- Re: Question about jumps
- From: Evenbit
- Re: Question about jumps
- From: randyhyde@xxxxxxxxxxxxx
- Re: Question about jumps
- From: Dragontamer
- Re: Question about jumps
- References:
- Question about jumps
- From: Markus Pitha
- Re: Question about jumps
- From: Evenbit
- Re: Question about jumps
- From: Evenbit
- Question about jumps
- Prev by Date: Re: Question about jumps
- Next by Date: Re: Question about jumps
- Previous by thread: Re: Question about jumps
- Next by thread: Re: Question about jumps
- Index(es):
Relevant Pages
|