News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Execution of HEX input, optimization

Started by xsi, January 25, 2013, 01:36:46 AM

Previous topic - Next topic

xsi

A program of 99 bytes:
00000000  6800B8            push word 0xb800
00000003  07                pop es
00000004  680000            push word 0x0
00000007  1F                pop ds
00000008  31FF              xor di,di
0000000A  31DB              xor bx,bx
0000000C  31C0              xor ax,ax
0000000E  CD16              int 0x16
00000010  B400              mov ah,0x0
00000012  2C30              sub al,0x30
00000014  91                xchg ax,cx
00000015  51                push cx
00000016  B90300            mov cx,0x3
00000019  81F90200          cmp cx,0x2
0000001D  7411              jz 0x30
0000001F  31C0              xor ax,ax
00000021  CD16              int 0x16
00000023  268807            mov [es:bx],al
00000026  43                inc bx
00000027  43                inc bx
00000028  81F90100          cmp cx,0x1
0000002C  7406              jz 0x34
0000002E  E2E9              loop 0x19
00000030  8BD0              mov dx,ax
00000032  E2E5              loop 0x19
00000034  80F841            cmp al,0x41
00000037  7D02              jnl 0x3b
00000039  7C05              jl 0x40
0000003B  80E837            sub al,0x37
0000003E  EB03              jmp short 0x43
00000040  80E830            sub al,0x30
00000043  80FA41            cmp dl,0x41
00000046  7D02              jnl 0x4a
00000048  7C05              jl 0x4f
0000004A  80EA37            sub dl,0x37
0000004D  EB03              jmp short 0x52
0000004F  80EA30            sub dl,0x30
00000052  C0E204            shl dl,byte 0x4
00000055  00D0              add al,dl
00000057  8805              mov [di],al
00000059  47                inc di
0000005A  59                pop cx
0000005B  E2B8              loop 0x15
0000005D  3EEA00000000      ds jmp word 0x0:0x0

Wasteful 2-times pass:
cmp al,0x41
jnl 0x3b
jl 0x40
sub al,0x37
jmp short 0x43
sub al,0x30

I have clumsy loops, especially in this part:
00000016  B90300            mov cx,0x3
00000019  81F90200          cmp cx,0x2
0000001D  7411              jz 0x30
0000001F  31C0              xor ax,ax
00000021  CD16              int 0x16
00000023  268807            mov [es:bx],al
00000026  43                inc bx
00000027  43                inc bx
00000028  81F90100          cmp cx,0x1
0000002C  7406              jz 0x34
0000002E  E2E9              loop 0x19
00000030  8BD0              mov dx,ax
00000032  E2E5              loop 0x19
00000034  ...
end
And no idea. And the converting algorithm works twice (with 2 JMPs). But the program works!
1 - enter quantity of bytes
2 - then enter those bytes
For example:
5
EA0000FFFF
reboot
:)

Tedd

You may get useful answers if you ask an actual question.

Also, do you understand this code? It almost looks like a random assortment of instructions have been thrown together.
Potato2

xsi

00000000  6800B8            push word 0xb800
00000003  07                pop es
00000004  680000            push word 0x0
00000007  1F                pop ds
00000008  31FF              xor di,di
0000000A  31DB              xor bx,bx
0000000C  31C0              xor ax,ax

; es(b800) - videobuffer - symbols will write just in it's memory
; data segment - the precise location to addres the would-be written HEX sequence

0000000E  CD16              int 0x16
00000010  B400              mov ah,0x0
00000012  2C30              sub al,0x30
00000014  91                xchg ax,cx

; Int 16 keyboard input symbol 1-9 (ascii 31-39) minus 30 = quantity of bytes in CX (for quantity of outer loops)

00000015  51                push cx  ;outer loop organization, loop point <
00000016  B90300            mov cx,0x3
00000019  81F90200          cmp cx,0x2
0000001D  7411              jz 0x30
0000001F  31C0              xor ax,ax
00000021  CD16              int 0x16
00000023  268807            mov [es:bx],al ;symbols that were input via int16 keyb to videobuffer to show on screen
00000026  43                inc bx
00000027  43                inc bx ;one byte on screen in buffer takes 2 bytes actually (2nd - the attribute)
00000028  81F90100          cmp cx,0x1
0000002C  7406              jz 0x34
0000002E  E2E9              loop 0x19
00000030  8BD0              mov dx,ax ; program firstly jumps here to save first symbol in dx
00000032  E2E5              loop 0x19

;after two cycles for two bytes of videobuffer it's time to write an ASCII to the data segment from 0x0 to N (input in cx, by user) - to repeat the outer cycle N times

00000034  80F841            cmp al,0x41
00000037  7D02              jnl 0x3b
00000039  7C05              jl 0x40
0000003B  80E837            sub al,0x37
0000003E  EB03              jmp short 0x43
00000040  80E830            sub al,0x30

;after first check in al , check in dl (first symbol)

00000043  80FA41            cmp dl,0x41
00000046  7D02              jnl 0x4a
00000048  7C05              jl 0x4f
0000004A  80EA37            sub dl,0x37
0000004D  EB03              jmp short 0x52
0000004F  80EA30            sub dl,0x30
00000052  C0E204            shl dl,byte 0x4

; shift to the upper nibble of byte of DL to add with the lower nibble of AL to form the right HEX (that was input in two bytes, have been showed before on the screen of videobuffer)

00000055  00D0              add al,dl
00000057  8805              mov [di],al
00000059  47                inc di


0000005A  59                pop cx
0000005B  E2B8              loop 0x15
0000005D  3EEA00000000      ds jmp word 0x0:0x0

Tedd

(This thread should be moved to the 16-bit sub-forum.)

Your request seems to be "I have this code, which inputs raw hex values into memory and then attempts to execute those bytes, could someone help me optimize and clean it up?" But first, a few questions..

1. Do you understand x86 assembler?
2. Did you write all of this code yourself? Did you copy parts? Did you have 'help?'
3. What is the code supposed to do? ('what', not 'how')
4. What is the purpose? (personal, homework, hacking, ...)
Potato2

dedndave

i doubt it's hacking - lol
it's 16-bit code
and - he appears to be trying to display chars from the keyboard directly to the video buffer
more likely homework

looks like disassembly of someone else's EXE
what the disassembly won't tell you is that the words at 5 and 61h are relocatables

frktons

There are only two days a year when you can't do anything: one is called yesterday, the other is called tomorrow, so today is the right day to love, believe, do and, above all, live.

Dalai Lama