PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] What is the assembly version of 1 times null/void/undefined?

 July 09, 2022     assembly, keyword, null, undefined, void     No comments   

Issue

To start off, i know what null void and not defining something do for the code, but a computer only knows binary. I can't tell it to do something i imagined and did not define in one way or the other. So lets assume the compiler would't get a heart attack of what i am trying to make it do - what would these examples look like in assembler or another way of showing the thing behind the keywords:

private int number;

public void method() {}

public int main() {
    int i = 1 * null;
    int j = 1 * number;
    int k = 1 * method();
}

Solution

C has no concept of a void value; that's an error. void* is just a compile-time-only placeholder for "unspecified, cast before using". i.e. a generic pointer value. It would be meaningless to dereference it in C or in asm, that's why compilers reject it.

An int is fixed width and uses all the bits to represent a number's value, like the simple / normal way of using a register in assembly. It can't hold anything else to indicate "null" or "undefined". If you want something like perl where you can undef $foo, you'd want something like C++ std::optional, like an <int, bool> pair of values. An int in most languages must be a number, not some "oops I'm not actually an int" indicator.

(Although rare ISAs do have trap values, e.g. one's complement machines that disallow the all-ones bit-pattern for signed integers, instead of having it work as a negative 0. But on most ISAs only floating-point values can be NaN - a special bit-pattern that means "not a number".)


C also doesn't have a null keyword. It has NULL as a preprocessor macro (which can be #defined as ( (void*)0 ) or even just 0). (Fun fact: ISO C actually allows implementations to use any bit-pattern they want as the runtime value of a null pointer, with compile-time translation of integer 0 in a pointer context to that bit-pattern.) But all normal implementations on modern CPUs just use 0. Like mov x0, #0 (AArch64). So at least one part of you question does have an answer.


So basically you can't do any of these things in C because there literally isn't any meaning to them, and you can't do them in asm either. There isn't some layer of magic underneath which C compilers are denying you access to.

Write C that can actually compile and look at the asm output (https://godbolt.org/ and How to remove "noise" from GCC/clang assembly output?)



Answered By - Peter Cordes
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing