flat assembler
Message board for the users of flat assembler.

Index > OS Construction > [SOLVED]Compare String, Invalid OpCode (BITS 32 :) )

Author
Thread Post new topic Reply to topic
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
sid123
I don't know why this code should hang on QEMU, and cause a Invalid Opcode on Bochs, works fine on 16 bit though.
Code:
strcmp:
        pusha
.cmp:
        ;; Put one char from ESI into AL
        mov al, [esi]
        ;; Put one of 'em in BL
        mov bl, [edi]
        ;; Compare them
        cmp al, bl
        jne .unequal
        ;; End of String?
        cmp al, 0
        je .equal_done
        inc esi
        inc edi
        jmp .cmp
.unequal:
        popa
        ;; Set EAX to unequal code (Plan to use a CLC here.)
        mov eax, -1
        ret
.equal_done:
        popa
        ;; Null out EAX, Plan to use a STC here
        xor eax, eax
        ret
    

I call it like this:
Code:
        mov eax, shell_input_buffer
        call GetString
        ;; Put Buffer in ESI for comparison
        mov esi, shell_input_buffer
.compare:
        ;; Now compare the strings
        mov edi, shell_help_str
        call strcmp
        cmp eax, 0
        je shell_help
        ;call printf32
        jmp shell_error
    

My Get String Function works fine as it should, as I can "echo" my string properly, I guess it's some weird problem here. Razz
I don't understand why does it cause an "Invalid OpCode Error", is there a restriction in 32-bit Mode for using this?
Cheers,
Sid

_________________
"Those who can make you believe in absurdities can make you commit atrocities" -- Voltaire https://github.com/Benderx2/R3X
XD


Last edited by sid123 on 15 Feb 2014, 16:10; edited 1 time in total
Post 13 Feb 2014, 10:48
View user's profile Send private message Reply with quote
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
if you need to save and restore the 32 bits registers:



pushad

popad



or, the better method (in my opinion):

push eax ebx ecx edx esi edi

pop edi esi edx ecx ebx eax
Post 13 Feb 2014, 13:39
View user's profile Send private message Visit poster's website Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
sid123
If I understand you correctly PUSHA is for 16 bit, PUSHAD is for 32 bit?
Post 13 Feb 2014, 13:44
View user's profile Send private message Reply with quote
baldr



Joined: 19 Mar 2008
Posts: 1651
baldr
sid123,

As fasm manual says, pusha mnemonic generates appropriate opcode for current code size (in other words, 0x60 always, except for 64-bit code ;-), whereas pushaw and pushad are more sensitive and generate operand-size override prefix on demand. This slightly deviates from Intel SDM/AMD APM though.

Bochs has internal debugger; QEMU can be configured for remote debugging as well.


Last edited by baldr on 15 Feb 2014, 14:35; edited 1 time in total
Post 13 Feb 2014, 22:53
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 17663
Location: In your JS exploiting you and your system
revolution
sid123: Your code does not show if the CPU is running in 32-bit or 16-bit mode. It also does not show if it is assembled in 32-bit or 16-bit mode. Example:
Code:
use32
mov esi, something
;...
use16
mov esi, something    
In both cases above the operation is changed to using SI (and two left over bytes) if the CPU mode does not match the assembled mode. For your code in the OP we have no way to know what is happening because the posted code is incomplete.

There are no apparent invalid opcodes in the code you posted so we are left guessing as to what is happening in the rest of the program. If you need more help then consider posting a complete example so that we can see what is happening.
Post 14 Feb 2014, 06:27
View user's profile Send private message Visit poster's website Reply with quote
SeproMan



Joined: 11 Oct 2009
Posts: 60
Location: Belgium
SeproMan
sid123,

I see you don't take into account the length of the second string.
Perhaps nothing is wrong with THIS code but is an error generated elsewhere.

_________________
Real Address Mode.
Post 15 Feb 2014, 14:30
View user's profile Send private message Reply with quote
sid123



Joined: 30 Jul 2013
Posts: 340
Location: Asia, Singapore
sid123
Hi,
Thanks for all sugestions. Smile
I finally found out that by tweaking my segments a lil';
I could use the 'rep cmpsb' instructon. I had to tweak
my GetString function to return the number of characters
recorded in ECX.
Thanks again for helping.
Cheers,
Sid
Post 15 Feb 2014, 16:02
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.