The trouble with assembly language is its built in obsolescence.<br><br><div><span class="gmail_quote">On 13/01/2008, <b class="gmail_sendername">Nicholas Thomas</b> <<a href="mailto:nick@lupine.me.uk">nick@lupine.me.uk
</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Dominic Hibbs wrote:<br>> POINTER HELP REQUESTED<br>><br>> I am an assembly language programmer - I also program in many other
<br>> languages BUT, as many of you youngsters may not yet have learned,<br>> Knowledge has a short shelf life and my C/C++ has been on the shelf<br>> too long.<br>> In this - "long int" means 64 bit.
<br>><br>> In assembly language there is no typing at all. A 32 bit quantity may be<br>> 1. an integer,<br>> 2. a sixteen bit number followed by a byte with no defined value and a<br>> character,<br>> 3. half of a double,
<br>> 4. four bytes of program.<br>> 5. a pointer to something,<br>> 6. or a pointer to a pointer<br>> and it is up to the programmer how something is interpreted and what<br>> alignment is used.<br>><br>
> I am converting a program, written in assembler to C/C++ and I am<br>> stuck over pointers. This data structure is a linked list of variables.<br>><br>> A block of memory consists of records made up of<br>>
<br>> Byte No. Content<br>> (0..7) a 32 bit pointer to<br>> a long int (8 Byte)<br>> or a double<br>> or (32 bit pointer and two 16 bit integers)<br>> (8..15) followed by one byte made up of
<br>> 2 bits of flags, 4 bits of a number and two more bit flag<br>> followed by an ASCIZ string of 4 n + 3 bytes long (n = 0..30)<br>> (16..23) followed by a long int or Double<br>> or (32 bit pointer and two 16 bit integers)
<br>><br>> Bits 8..15 are anded with & 0x00 00 00 3c and then added to the<br>> address of this record to get the next record.<br>><br>> First variable is a long int called "ten" and has the value 10 so the
<br>> first two bits of byte 8 hav the value 0b01 and the record length is 20<br>> bytes the other two bit flags have the value 0b01 (0 means Not an array<br>> and the 1 bit says this variable has been initialised).
<br>><br>> Abit of (obviously incorrect) code follows:<br>><br>> char * s = "ten";<br>> void * blockaddr = malloc(varspace); // Check return value for NULL<br>> recptr = blockaddr; // get address of first record
<br>> * recptr = * recptr + 16; // insert into memory at blockaddr<br>> // blockaddress + 16<br>> recptr += 8; // pointer arithmetic point to byte 8<br>><br>> * recptr = 1 | 20 | 0x80 // two bit flags | record length |
<br>> // initialised as a byte value<br>> recptr++;<br>> *recptr++ = *s; // recptr is a char *<br>> char ch;<br>> do {<br>> *recptr++ = ch = * (++ s);
<br>> } while( ch);<br>> recptr +=3; // align on four byte -<br>> // we are at the start of the next<br>> record.<br>><br>><br>Urgh. Ouch. Eeep. Tell me, is there any particular reason you need to
<br>emulate the program's memory allocation behaviour so precisely? If this<br>were me, I'd be looking at using (probably) std::list, containing either<br>a (fairly) simple class or a struct/union thingy (which I haven't used
<br>for ages). Trying to literally convert an ASM program to C/C++ sounds<br>like a lesson in pain (why not just use asm { ... } in that case? ;) ).<br><br>Also, you can't guarantee that long int is 64 bits. As an example:
<br><lupine_85> geordi: {long int i; cout << sizeof(i); }<br><geordi> 4<br><lupine_85> geordi: {long long i; cout << sizeof(i); }<br><geordi> ISO C++ does not support 'long long'
<br><lupine_85> geordi: {long long int i; cout << sizeof(i); }<br><geordi> ISO C++ does not support 'long long'<br><br>If you have <stdint.h>, you can use uint64_t (or int64_t) instead.<br>
<br>/Nick<br><br><br>_______________________________________________<br>York mailing list<br><a href="mailto:York@lists.lug.org.uk">York@lists.lug.org.uk</a><br><a href="https://mailman.lug.org.uk/mailman/listinfo/york">https://mailman.lug.org.uk/mailman/listinfo/york
</a><br></blockquote></div><br>