LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-12-2007, 04:57 AM   #1
telnet_ping
LQ Newbie
 
Registered: Aug 2007
Distribution: debian etch
Posts: 12

Rep: Reputation: 0
Question how is zero flags set


when and how are zero flag set..
plzz help me out!!!
wat is H.O bits...??
 
Old 09-12-2007, 08:22 AM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
telnet_ping, you should provide more context to your question. You've posted to a programming forum, so evidently, this is a programming question. We need more information about things like the language you are using, any particular libraries that might be related, the nature of the program you are trying to write, etc.
A code snippet (in code tags, please) and any error messages that are emitted by compilers, linkers, runtime errors, and such would probably be useful, as well.
--- rod.
 
Old 09-12-2007, 01:01 PM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
I take it you are taking an assembly class and have a quiz coming up or something? The zero flag is set whenever the result of some operations are zero. For instance:
Code:
mov ax,10
sub ax,10
Would set the zero flag. Mostly these are arithmetic instructions, such as add, sub, mul, etc. It is also affected by some other instructions; most notable, cmp (which essentially does a subtraction anyways, just doesn't store the result).

H.O. bits are "High order bits". The name is somewhat self explanatory, but, for example, take the byte 00111100 as an example. The high order bits are 0011 (the highest half). The other half is the low order bits (1100). When actually programming in assembly, the high order bits don't necessarily come first. The x86, for example, is what is called "little endian". This means that it stores its low order bytes first in memory (notice bytes, not bits). This means, a 32 bit integers like: 01011111 00000011 11000000 00000000 is actually stored as 00000000 11000000 00000011 01011111 (bytes are reversed).

If you don't have a text book for your course, you should buy one now. Any book should have this information covered in detail (ie. read it). You can even download a free book; The Art of Assembly (google for it).

Last edited by 95se; 09-12-2007 at 01:02 PM.
 
Old 09-12-2007, 11:41 PM   #4
telnet_ping
LQ Newbie
 
Registered: Aug 2007
Distribution: debian etch
Posts: 12

Original Poster
Rep: Reputation: 0
thank you very much 95se

i just need to code in assembly that is y. so i thought of learning the basis. i have some more to ask....
will the carry flag set for this operation and y?
Code:
mov ax,5
and ax,4
and u said about little Endian.. is there any advantage for little over big?? or vice versa???

i dint get the explanation for SAR & SHR ...
it says .... SAR is used for signed division and its said that SAR truncate to -infinity were as SHR to positive.
and why is -15>>2 is -4
and 15>>2 3 can u specify the explanation in bit patterens


and finaly.....
now i learned to write assembly coding.. so could u plz tell me how to compile .asm file and start executing......
i am using debian kernel 2.6
thank you very much!!

Last edited by telnet_ping; 09-13-2007 at 12:45 AM.
 
Old 09-12-2007, 11:47 PM   #5
telnet_ping
LQ Newbie
 
Registered: Aug 2007
Distribution: debian etch
Posts: 12

Original Poster
Rep: Reputation: 0
i was writing for 80x86

Quote:
Originally Posted by theNbomr View Post
telnet_ping, you should provide more context to your question. You've posted to a programming forum, so evidently, this is a programming question. We need more information about things like the language you are using, any particular libraries that might be related, the nature of the program you are trying to write, etc.
A code snippet (in code tags, please) and any error messages that are emitted by compilers, linkers, runtime errors, and such would probably be useful, as well.
--- rod.
consider the snipper
Code:
mov ax,5
and ax,4
i dint complied i asked this out of curiosity... since there these thing are programming basics i had posted in this forum...
 
Old 09-14-2007, 08:06 PM   #6
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Glad to see you're learning assembly, definitely helps w/ programming in general.

The most basic question should really be answered first.

Quote:
Originally Posted by telnet_ping View Post
and finaly.....
now i learned to write assembly coding.. so could u plz tell me how to compile .asm file and start executing......
i am using debian kernel 2.6
thank you very much!!
Firstly, I use NASM. It is a GREAT assembler and works on many OS (on x86 & AMD64/EMT64. Start w/ a basic NASM program (I'm assuming you are using linux)...

nothing.asm (works on both 32 and 64 bit)
Code:
global _start

section .text

_start:
mov ebx, 0   ; return code
mov eax, 1   ; exit
int 0x80     ; linux interrupt
Now assemble it to an ELF object file (32 bit)
Code:
nasm -f elf nothing.asm
or 64 bit.
Code:
nasm -f elf64 nothing.asm
This should create a file nothing.o. Now link it and execute it.
Code:
ld -s -o nothing nothing.o
./nothing
You can also link it to the C library. Here is a quicky I wrote to test if various instructions set the zero flag.

test_zero.asm (64 bit)
Code:
cpu X64

extern puts

global main:function

section .data

yes      db "Zero flag is set", 0
no       db "Zero flag is not set", 0

section .text

main:

xor eax, eax     ; eax = 0, bitwise binary operators set zero flag
call check_zero  ; YES
xor eax, eax
inc eax	         ; zero flag cleared
mov eax, 10      ; mov instruction doesn't touch zero flag
call check_zero  ; NO
inc eax          ; inc is arithmetic, eax + 1 = 11
call check_zero  ; NO
sub eax, 11      ; arithmetic, eax - 11 = 0
call check_zero  ; YES
add eax, 12      ; add is arithmetic, eax = 12, zero flag is cleared
and eax, 0       ; bitwise binary operators set zero flag (if 0)
call check_zero  ; YES
mov eax, 9       ; eax = 9
xor ebx, ebx     ; ebx = 0
mul ebx          ; eax = eax * ebx = 0
call check_zero  ; YES
mov ebx, 8       ; ebx = 8
mov eax, 8       ; eax = 8
cmp eax, ebx     ; eax == ebx (eax - ebx = 0)
call check_zero  ; YES

ret

check_zero:
push rdi
push rbp
mov rbp,rsp

mov rdi, no
jnz .print_no
mov rdi, yes
.print_no
push rax
call puts
pop rax

mov rsp,rbp
pop rbp
pop rdi
ret
To assemble/link it.
Code:
nasm -f elf64 test_zero.asm
gcc -o test_zero test_zero.o
You should really read up on the calling convention differences between 64 and 32 bit (in a nutshell, on 32 everything is pushed onto the stack, on amd64 the args are stored in rdi, rsi, rdx, rcx, r8, and r9 (xmm0-7 for floating point args) first, then pushed onto the stack).

Quote:
Originally Posted by telnet_ping View Post
will the carry flag set for this operation and y?
Try modifying the program I gave you above and find out (jnc instead of jnz).

Quote:
Originally Posted by telnet_ping View Post
and u said about little Endian.. is there any advantage for little over big?? or vice versa???
They are both found in the wild (x86 is little endian, Sparc, PPC, and ARM are big endian). Technically, I wouldn't say one is better than the other. In terms of programming, little endian does let you cast a bit easier (since the same memory address could be used when an int is cast to a char for instance -- think why), but I would not say that makes it better in any way. Big endianess is a bit more logical, since it is what would be expected.

Quote:
i dint get the explanation for SAR & SHR ...
it says .... SAR is used for signed division and its said that SAR truncate to -infinity were as SHR to positive.
and why is -15>>2 is -4
and 15>>2 3 can u specify the explanation in bit patterens
GET THE NASM DOCUMENTATION. It has a list of all x86 instructions along w/ a description (in the appendix). SHR always uses 0 to fill in the spots left by shifting the buts to the right. SAR takes the left most bit, and uses that to fill in the spots left by shifting to the right. Think about how negative numbers are stored, then think about what -15 would look like shifted to the right twice using SHR and then SAR.

Links:
NASM Downloads (including docs): http://sourceforge.net/project/showf...?group_id=6208
NASM Homepage: http://nasm.sourceforge.net/

BTW!!!! If you have a amd64 or emt64 (Intel), you will need to use NASM 0.99.02. 0.98.* only supports x86 (32 bit), and this is the stable version. Most linux distros will have packages for 0.98.* rather than 0.99.02, so you may need to compile 0.99.02 from the source.

Last edited by 95se; 09-14-2007 at 08:16 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GCC Flags matsko Linux - Software 2 05-14-2007 01:23 PM
How do you set compiler flags in SuSE? Thaidog SUSE / openSUSE 0 12-26-2006 10:04 PM
Process Flags kikette Linux - General 2 10-31-2006 05:15 AM
can I set these flags using a shell script nirav.jani Linux - Security 1 01-12-2005 06:01 PM
CC flags and the like Garp Linux - Software 2 05-17-2004 03:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:33 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration