Reputation: 31
I want to check the endianness of my computer in an assembly program ( 8086 ) and to print it.
I know what endianness is and how you find it. It is about the way data is stored in memory. For eg if you store 1234 it will be stored 3412 ( little ) or 1234 ( big ). But i don't know how to do it in assembly language. I'm thinking about storing something in memory than load it and compare it with the original. It should be exactly as the theory says. If i store 1234 i will load it as 3412 or 1234. I have a problem when i'm trying to create an if statement.
litte db "little$"
big db "big$"
mov ax, var1
cmp ax, var2
je equal
jmp notequal
equal:
lea dx, big
notequal:
lea dx, little
I expect it to print little or big, but no matter what values i put in var1 and var 2 it always prints little, because somehow the code is executed line by line. Shouldn't it go to only one label, "equal" or "notequal"? I want something like:
if var1==var2
print big
else
print little
Upvotes: 2
Views: 1201
Reputation: 882686
This segment will always print "little" since, even if you begin execution at equal
, it will carry on through notequal
:
equal:
lea dx, big
notequal:
lea dx, little
What you would need is something like:
equal:
lea dx, big
jmp done
notequal:
lea dx, little
done:
; carry on
Additionally, you should store the word 1234h
into some memory location y
, then read the byte back from that same location.
If 12h
then you are big-endian. The value 34h
means little-endian.
Anything else means you have a memory problem :-)
By the way, I'm pretty certain all x86 CPUs are little-endian so, if you're writing this in x86 assembly language, you probably don't need to check.
By way of example, you can use Coding Ground with the following code to see it in action:
section .text
global _start
_start:
mov ax, 0x1234 ; load up 1234 hex
mov [myword], ax ; store that word to memory
mov al, [myword] ; get first byte of that
cmp al, 0x12 ; 12 means big endian
je big
little:
mov edx, l_len ; prepare for little message
mov ecx, l_msg
jmp print
big:
mov edx, b_len ; prepare for little message
mov ecx, b_msg
print:
mov ebx, 1 ; file handle 1 = stdout
mov eax, 4 ; 'syswrite' function call
int 0x80
mov eax, 1 ; 'exit' function call
int 0x80
section .data
myword dw 0
l_msg db 'Little endian', 0xa
l_len equ $ - l_msg
b_msg db 'Big endian', 0xa
b_len equ $ - b_msg
The output window shows, as expected:
$ nasm -f elf *.asm; ld -m elf_i386 -s -o demo *.o
$ demo
Little endian
Upvotes: 3