HEX Conversion (little-endian) - hex

I hope someone may be able to help me!
I am trying to work out how the following was done. The Hex below is stored as little-endian:
ff3f fc7f 0040 0000 ffff f17f 0000 0280
I am trying to understand how the author converted (or re-wrote) this to:
7FFC 3FFF 0000 4000 7FF1 FFFF 8002 0000
I have scoured the internet but I am at a loss :(
Thanks in advance for all that can help!

Here is convertion functions for different data types (your case is unsigned int):
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
inline void endian_swap(unsigned short& x)
{
x = (x>>8) |
(x<<8);
}
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
inline void endian_swap(unsigned int& x)
{
x = (x>>24) |
((x<<8) & 0x00FF0000) |
((x>>8) & 0x0000FF00) |
(x<<24);
}
//-----------------------------------------------------------------------------------------------------
//
//-----------------------------------------------------------------------------------------------------
inline void endian_swap(unsigned __int64& x)
{
x = (x>>56) |
((x<<40) & 0x00FF000000000000) |
((x<<24) & 0x0000FF0000000000) |
((x<<8) & 0x000000FF00000000) |
((x>>8) & 0x00000000FF000000) |
((x>>24) & 0x0000000000FF0000) |
((x>>40) & 0x000000000000FF00) |
(x<<56);
}

Related

equivalent of objdump for dex files

Is there any equivalent to objdump for dex files?
I don't want to decompile them, just be able to watch their content
Just in case it helps someone in the future:
$ sudo apt install dexdump
baksmali has a dump command that generates an annotated hex dump of a dex file
baksmali dump helloworld.dex > helloworld.txt
|-----------------------------
|code_item section
|-----------------------------
|
|[0] code_item: LHelloWorld;->main([Ljava/lang/String;)V
0001c4: 0200 | registers_size = 2
0001c6: 0100 | ins_size = 1
0001c8: 0200 | outs_size = 2
0001ca: 0000 | tries_size = 0
0001cc: 0000 0000 | debug_info_off = 0x0
0001d0: 0800 0000 | insns_size = 0x8
| instructions:
0001d4: 6200 0000 | sget-object v0, Ljava/lang/System;->out:Ljava/io/PrintStream;
0001d8: 1a01 0000 | const-string v1, "Hello World!"
0001dc: 6e20 0100 1000 | invoke-virtual {v0, v1}, Ljava/io/PrintStream;->println(Ljava/lang/String;)V
0001e2: 0e00 | return-void
It is also able to list the items in some of the various constant pools
baksmali list classes helloworld.dex
LHelloWorld;
baksmali list methods helloworld.dex
LHelloWorld;->main([Ljava/lang/String;)V
Ljava/io/PrintStream;->println(Ljava/lang/String;)V
baksmali list fields helloworld.dex
Ljava/lang/System;->out:Ljava/io/PrintStream;
baksmali list strings helloworld.dex
"Hello World!"
"LHelloWorld;"
"Ljava/io/PrintStream;"
"Ljava/lang/Object;"
"Ljava/lang/String;"
"Ljava/lang/System;"
"V"
"VL"
"[Ljava/lang/String;"
"main"
"out"
"println"

disable editing and setItem to several rows in QTableWidget

| Name | Math | Physics | Average grade |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
the column "math" and "physics" is for user to put number in, the column "average" CAN NOT be put value, it is only for showing average value after user put "math" and "physics" grade. How can i disable editing but still show calculated value in column "average" ?
I want to write content to several rows, but my code does not work
ui->tableWidget->setColumnCount(4);
ui->tableWidget->setRowCount(8);
ui->tableWidget->setSortingEnabled(false);
QTableWidgetItem *newItem = new QTableWidgetItem(tr("Hello World"));
for (int i=0; i< ui->tableWidget->rowCount(); i++)
ui->tableWidget->setItem(i, 0, newItem);
There is a message like this
QTableWidget: cannot insert an item that is already owned by another QTableWidget
I still don't understand because i am a newbie with QT. Please help me, many thanks ! :)
Code updated:
for (int i = 0; i < ui.tableWidget->rowCount(); i++) {
QTableWidgetItem *icon_item = new QTableWidgetItem;
QIcon icon(":/myproject/icon/test.png");
icon_item->setIcon(icon);
ui.tableWidget->setItem(i, 0, icon_item);
}
for (int j = 0; j < ui.tableWidget->rowCount(); j++) {
ui.tableWidget->item(j, 3)->setFlags(ui.tableWidget->item(j, 3)->flags() & ~Qt::ItemIsEditable);
}
You can use QTableWidgetItem::setFlags(). To keep the other default flags, but remove Qt::ItemIsEditable:
item->setFlags(item->flags() & ~Qt::ItemIsEditable);
You cannot set the same QTableWidgetItem instance to multiple cells. To get you started:
for (...) {
QTableWidgetItem *item = new QTableWidgetItem(...);
tableWidget->setItem(...);
}

teensy pointer on input string

Currently a student in college, decided to jump ahead of my programming class and have a little fun with pointers. This is supposed to take a specific serial input and change the state of three LED's I have attached to the Teensy++2.0. However it seems to be just giving me back the first input.
http://arduino.cc/en/Serial/ReadBytesUntil
This is my reference for the ReadBytesUntil() The input goes #,#,### (1,1,255 being an example)
I guess basically my question is, does ReadBytesUntil() deal with commas? And if so, whats going on here?
EDIT -- I asked my teacher and even he has no clue why it doesn't work.
char *dataFinder(char *str){
while (*str != ','){
str++;
}
str++;
return str;
}
void inputDecoder(){
str = incomingText;
whichLED = *str;
dataFinder(str);
onoff = *str;
dataFinder(str);
powerLevel = *str;
}
void loop(){
int length;
if (Serial.available() > 0 ){ //this is basically: if something is typed in, do something.
length = Serial.readBytesUntil(13,incomingText, 10); //reads what is typed in, and stores it in incomingVar
incomingText[length]=0; ///swapping out cr with null
inputDecoder();
//ledControl();
Serial.print("Entered:");
//incomingText[9]=0;
Serial.println(incomingText); //Here for testing, to show what values I'm getting back.
Serial.println(whichLED);
Serial.println(onoff);
Serial.println(powerLevel);
}
delay(1000);
}
The str in inputDecoder() is from the global scope and is not the same str in dataFinder(), which has local scope.
Imagine this ASCII picture is the layout of memory:
str
+-----+-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| * | | | | ... | 1 | , | 1 | , | 2 | 5 | 5 | \n |
+--|--+-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
|
|
\-----------------------------^
When you pass str to dataFinder() it creates a copy of the pointer, which I'll call str'
str str'
+-----+-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| * | | * | | ... | 1 | , | 1 | , | 2 | 5 | 5 | \n |
+--|--+-----+--|--+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| \-----------------^
|
\-----------------------------^
When dataFinder() increments str it is really altering str'
str str'
+-----+-----+-----+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| * | | * | | ... | 1 | , | 1 | , | 2 | 5 | 5 | \n |
+--|--+-----+--|--+-----+ +-----+-----+-----+-----+-----+-----+-----+-----+
| \-----------------------------^
|
\-----------------------------^
Then, when you return to inputDecoder() you dereference str which is still pointing at the start of the string.
You can either assign the value of str' back to the global str using:
str = dataFinder(str);
or change dataFinder() so it does not take an argument, therefore not copying the variable.

How can I extract the MCC and MNC from a PLMN?

I am running the AT command AT+KCELL to get cell information and it returns, amongst other things, a PLMN (Public Land and Mobile Network) - the description of this from the documentation is:
PLMN identifiers (3 bytes), made of MCC (Mobile Country Code), and MNC
(Mobile Network Code).
OK, that matches what Wikipedia says - in there is the MCC and MNC. Now what I don't understand is how do I extract the aforementioned MCC and MNC values?
Here is an example. I get back:
32f210
and I am told (though I am sceptical) that that should result in:
MNC: 1
MCC: 232
but I can't for the life of me work out how to get that result from the PLMN so how do I parse this?
Well, I have found this out and figured I would add an answer here in case there is some other unlucky soul who has to do this - the PDF called GSM Technical Specification (section 10.2.4) contains the answer, the relevant bit is:
PLMN Contents: Mobile Country Code (MCC) followed by the Mobile Network Code (MNC). Coding: according to TS GSM 04.08 [14].
If storage
for fewer than the maximum possible number n is required, the excess
bytes shall be set to 'FF'. For instance, using 246 for the MCC and 81
for the MNC and if this is the first and only PLMN, the contents reads
as follows: Bytes 1-3: '42' 'F6' '18' Bytes 4-6: 'FF' 'FF' 'FF' etc.
So I was wrong to be sceptical!
I need to read from the left swapping the digits around so the first two bytes would be the MCC so that would be 232f and the MNC would be 01 then I just discard the f and I have 232 and 1! Glad that one is sorted.
For example, in c# you can do it like this:
string plmn = "whatever the plmn is";
string mcc = new string(plmn.Substring(0, 2).Reverse().ToArray())
+ new string(plmn.Substring(2, 2).Reverse().ToArray())
.Replace('f', ' ')
.Trim();
string mnc = new string(plmn.Substring(4, 2).Reverse().ToArray())
.Replace('f', ' ')
.Trim();
Here is a java answer with bitwise operations:
public static String mcc(byte[] plmnId) {
if (plmnId == null || plmnId.length != 3)
throw new IllegalArgumentException(String.format("Wrong plmnid %s", Arrays.toString(plmnId)));
int a1 = plmnId[0] & 0x0F;
int a2 = (plmnId[0] & 0xF0) >> 4;
int a3 = plmnId[1] & 0x0F;
return "" + a1 + a2 + a3;
}
public static String mnc(byte[] plmnId) {
if (plmnId == null || plmnId.length != 3)
throw new IllegalArgumentException(String.format("Wrong plmnid %s", Arrays.toString(plmnId)));
int a1 = plmnId[2] & 0x0F;
int a2 = (plmnId[2] & 0xF0) >> 4;
int a3 = (plmnId[1] & 0xF0) >> 4;
if (a3 == 15)
return "" + a1 + a2;
else
return "" + a1 + a2 + a3;
}

Z80 DAA instruction

Apologies for this seemingly minor question, but I can't seem to find the answer anywhere - I'm just coming up to implementing the DAA instruction in my Z80 emulator, and I noticed in the Zilog manual that it is for the purposes of adjusting the accumulator for binary coded decimal arithmetic. It says the instruction is intended to be run right after an addition or subtraction instruction.
My questions are:
what happens if it is run after another instruction?
how does it know what instruction preceeded it?
I realise there is the N flag - but this surely wouldnt definitively indicate that the previous instruction was an addition or subtraction instruction?
Does it just modify the accumulator anyway, based on the conditions set out in the DAA table, regardless of the previous instruction?
Does it just modify the accumulator anyway, based on the conditions set out in the DAA table, regardless of the previous instruction?
Yes. The documentation is only telling you what DAA is intended to be used for. Perhaps you are referring to the table at this link:
--------------------------------------------------------------------------------
| | C Flag | HEX value in | H Flag | HEX value in | Number | C flag|
| Operation | Before | upper digit | Before | lower digit | added | After |
| | DAA | (bit 7-4) | DAA | (bit 3-0) | to byte | DAA |
|------------------------------------------------------------------------------|
| | 0 | 0-9 | 0 | 0-9 | 00 | 0 |
| ADD | 0 | 0-8 | 0 | A-F | 06 | 0 |
| | 0 | 0-9 | 1 | 0-3 | 06 | 0 |
| ADC | 0 | A-F | 0 | 0-9 | 60 | 1 |
| | 0 | 9-F | 0 | A-F | 66 | 1 |
| INC | 0 | A-F | 1 | 0-3 | 66 | 1 |
| | 1 | 0-2 | 0 | 0-9 | 60 | 1 |
| | 1 | 0-2 | 0 | A-F | 66 | 1 |
| | 1 | 0-3 | 1 | 0-3 | 66 | 1 |
|------------------------------------------------------------------------------|
| SUB | 0 | 0-9 | 0 | 0-9 | 00 | 0 |
| SBC | 0 | 0-8 | 1 | 6-F | FA | 0 |
| DEC | 1 | 7-F | 0 | 0-9 | A0 | 1 |
| NEG | 1 | 6-F | 1 | 6-F | 9A | 1 |
|------------------------------------------------------------------------------|
I must say, I've never seen a dafter instruction spec. If you examine the table carefully, you will see that the effect of the instruction depends only on the C and H flags and the value in the accumulator -- it doesn't depend on the previous instruction at all. Also, it doesn't divulge what happens if, for example, C=0, H=1, and the lower digit in the accumulator is 4 or 5. So you will have to execute a NOP in such cases, or generate an error message, or something.
Just wanted to add that the N flag is what they mean when they talk about the previous operation. Additions set N = 0, subtractions set N = 1. Thus the contents of the A register and the C, H and N flags determine the result.
The instruction is intended to support BCD arithmetic but has other uses. Consider this code:
and 15
add a,90h
daa
adc a,40h
daa
It ends converting the lower 4 bits of A register into the ASCII values '0', '1', ... '9', 'A', 'B', ..., 'F'. In other words, a binary to hexadecimal converter.
I found this instruction rather confusing as well, but I found this description of its behavior from z80-heaven to be most helpful.
When this instruction is executed, the A register is BCD corrected using the contents of the flags. The exact process is the following: if the least significant four bits of A contain a non-BCD digit (i. e. it is greater than 9) or the H flag is set, then $06 is added to the register. Then the four most significant bits are checked. If this more significant digit also happens to be greater than 9 or the C flag is set, then $60 is added.
This provides a simple pattern for the instruction:
if the lower 4 bits form a number greater than 9 or H is set, add $06 to the accumulator
if the upper 4 bits form a number greater than 9 or C is set, add $60 to the accumulator
Also, while DAA is intended to be run after an addition or subtraction, it can be run at any time.
This is code in production, implementing DAA correctly and passes the zexall/zexdoc/z80test Z80 opcode test suits.
Based on The Undocumented Z80 Documented, pag 17-18.
void daa()
{
int t;
t=0;
// 4 T states
T(4);
if(flags.H || ((A & 0xF) > 9) )
t++;
if(flags.C || (A > 0x99) )
{
t += 2;
flags.C = 1;
}
// builds final H flag
if (flags.N && !flags.H)
flags.H=0;
else
{
if (flags.N && flags.H)
flags.H = (((A & 0x0F)) < 6);
else
flags.H = ((A & 0x0F) >= 0x0A);
}
switch(t)
{
case 1:
A += (flags.N)?0xFA:0x06; // -6:6
break;
case 2:
A += (flags.N)?0xA0:0x60; // -0x60:0x60
break;
case 3:
A += (flags.N)?0x9A:0x66; // -0x66:0x66
break;
}
flags.S = (A & BIT_7);
flags.Z = !A;
flags.P = parity(A);
flags.X = A & BIT_5;
flags.Y = A & BIT_3;
}
For visualising the DAA interactions, for debugging purposes, I have written a small Z80 assembly program, that can be run in an actual ZX Spectrum or in an emulation that emulates accurately DAA: https://github.com/ruyrybeyro/daatable
As how it behaves, got a table of flags N,C,H and register A before and after DAA produced with the aforementioned assembly program: https://github.com/ruyrybeyro/daatable/blob/master/daaoutput.txt

Resources