1

List the SHORTEST POSSIBLE CODE (counting number of instructions) for making x, y, and z, defined as follows, get the value 1. for an 80*86 machine

x:   dw   0xff00
y:   resb 1
z:   resw 1

edit: I think the answer should be somethink like that:

MOV  DWORD [x+1], 0x01010001  ; 

;check:

mov eax , 0
mov al , byte[y]
print_d eax ; print 0

mov eax , 0
mov ax , word[x]
print_d eax ; print 256

mov eax , 0
mov ax , word[z]
print_d eax ; print 257 

but. it is not good...sholud print 1

2
  • 2
    It's not clear what's being asked. Does that code have to initialize x, y and z? What does it mean "get the value 1"? What CPU is this for? If it's x86, what mode? 16-bit? 32-bit? 64-bit? If it's 16-bit, can 32-bit instructions be used? Commented Jun 25, 2012 at 8:23
  • i edit it. "get the value 1" mean that after the instruction x=y=z=1 Commented Jun 25, 2012 at 8:32

1 Answer 1

1

Here's the memory where your x, y and z are, listed as bytes (from lower addresses (x) to higher (z)):

xx XX yy zz ZZ

where xx is the least significant byte of x (0), XX is the most significant byte of x (0xFF) and likewise for y and z.

If I understand it correctly, y and z aren't initialized (res* hints NASM syntax for memory reservation keywords).

So you want to transform this:

00 FF yy zz ZZ

into this:

01 00 01 01 00

Right?

MOV DWORD [x+1], 0x01010001 will transform it into:

00 01 00 01 01

So, it's not correct. And you need more than 1 instruction to change 5 bytes because 32-bit instructions write at most 4 bytes at a time.

I'd say the shortest in terms of the number of instructions is 2 MOVs (NASM syntax):

mov dword [x], 0x01010001
mov byte [x+4], 0
Sign up to request clarification or add additional context in comments.

6 Comments

thank u!! what about Motorola 68000? is it true: MOVEQ.L #0x00010100, x MOVEQ.B #0x01, x+4
I'm not very familiar with Motorola. And you originally asked about x86.
For the example (assuming a "NASM like" syntax), x: dw 0 implies that x is in an initialised data section (e.g. .data); and y and z use resb/resw which implies that they are in an uninitialised data section (e.g. .bss). I don't see anything that guarantees that the address of x is anywhere close to the addresses of the other variables. The difference might be caused by a simple typo or something though.
@Brendan NASM does not care much about sections. Unless you explicitly tell it to change the current section, everything goes into the same current section, whatever it is, despite it being code, initialized or uninitialized data.
@AlexeyFrunze: Please attempt to use resw in an initialised data section, then attempt to use dw in an uninitialised data section; then remind me again just how much NASM didn't care about sections.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.